1 //===- ARMELFAttributeData.h ----------------------------------------------===//
2 //
3 // The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "ARMELFAttributeData.h"
10
11 #include <mcld/LinkerConfig.h>
12 #include <mcld/MC/Input.h>
13 #include <mcld/Support/LEB128.h>
14 #include <mcld/Support/MsgHandling.h>
15
16 using namespace mcld;
17
getAttributeValue(TagType pTag) const18 const ELFAttributeValue *ARMELFAttributeData::getAttributeValue(TagType pTag) const
19 {
20 if (pTag <= Tag_Max) {
21 const ELFAttributeValue &attr_value = m_Attrs[pTag];
22
23 if (attr_value.isInitialized()) {
24 return &attr_value;
25 } else {
26 // Don't return uninitialized attribute value.
27 return NULL;
28 }
29 } else {
30 UnknownAttrsMap::const_iterator attr_it = m_UnknownAttrs.find(pTag);
31
32 if (attr_it == m_UnknownAttrs.end()) {
33 return NULL;
34 } else {
35 return &attr_it->second;
36 }
37 }
38 }
39
40 std::pair<ELFAttributeValue*, bool>
getOrCreateAttributeValue(TagType pTag)41 ARMELFAttributeData::getOrCreateAttributeValue(TagType pTag)
42 {
43 ELFAttributeValue *attr_value = NULL;
44
45 if (pTag <= Tag_Max) {
46 attr_value = &m_Attrs[pTag];
47 } else {
48 // An unknown tag encounterred.
49 attr_value = &m_UnknownAttrs[pTag];
50 }
51
52 assert(attr_value != NULL);
53
54 // Setup the value type.
55 if (!attr_value->isUninitialized()) {
56 return std::make_pair(attr_value, false);
57 } else {
58 attr_value->setType(GetAttributeValueType(pTag));
59 return std::make_pair(attr_value, true);
60 }
61 }
62
GetAttributeValueType(TagType pTag)63 unsigned int ARMELFAttributeData::GetAttributeValueType(TagType pTag)
64 {
65 // See ARM [ABI-addenda], 2.2.6.
66 switch (pTag) {
67 case Tag_compatibility: {
68 return (ELFAttributeValue::Int | ELFAttributeValue::String);
69 }
70 case Tag_nodefaults: {
71 return (ELFAttributeValue::Int | ELFAttributeValue::NoDefault);
72 }
73 case Tag_CPU_raw_name:
74 case Tag_CPU_name: {
75 return ELFAttributeValue::String;
76 }
77 default: {
78 if (pTag < 32)
79 return ELFAttributeValue::Int;
80 else
81 return ((pTag & 1) ? ELFAttributeValue::String :
82 ELFAttributeValue::Int);
83 }
84 }
85 // unreachable
86 }
87
88 //===--------------------------------------------------------------------===//
89 // Helper Functions for merge()
90 //===--------------------------------------------------------------------===//
91
92 namespace {
93
94 /*
95 * Helper function to decode value in Tag_also_compatible_with.
96 *
97 * @ref ARM [ABI-addenda], 2.3.7.3
98 */
99 static int
decode_secondary_compatibility_attribute(const ELFAttributeValue & pValue)100 decode_secondary_compatibility_attribute(const ELFAttributeValue &pValue)
101 {
102 // The encoding of Tag_also_compatible_with is:
103 //
104 // Tag_also_compatible_with (=65), NTSB: data
105 //
106 // The data can be either an ULEB128-encoded number followed by a NULL byte or
107 // a NULL-terminated string. Currently, only the following byte sequence in
108 // data are currently defined:
109 //
110 // Tag_CPU_arch (=6) [The arch] 0
111 assert((pValue.type() == ELFAttributeValue::String) &&
112 "Value of Tag_also_compatible_with must be a string!");
113
114 const std::string &data = pValue.getStringValue();
115
116 // Though the integer is in LEB128 format, but they occupy only 1 byte in
117 // currently defined value.
118 if (data.length() < 2)
119 // Must have a byte for Tag_CPU_arch (=6)
120 // a byte for specifying the CPU architecture (CPU_Arch_ARM_*)
121 //
122 // Currently, the 2nd byte can only be v4T (=2) or v6-M (=11).
123 return -1;
124
125 if ((static_cast<uint8_t>(data[0]) == ARMELFAttributeData::Tag_CPU_arch) &&
126 ((data[1] == ARMELFAttributeData::CPU_Arch_ARM_V4T) ||
127 (data[1] == ARMELFAttributeData::CPU_Arch_ARM_V6_M)))
128 return static_cast<uint32_t>(data[1]);
129
130 // Tag_also_compatible_with can be safely ignored.
131 return -1;
132 }
133
134 /*
135 * This helper array keeps the ordering of the values in attributes such as
136 * Tag_ABI_align_needed which are sored as 1 > 2 > 0.
137 */
138 static const int value_ordering_120[] = { 0, 2, 1 };
139
140 } // anonymous namespace
141
142 //===--------------------------------------------------------------------===//
143 // End Helper Functions for merge()
144 //===--------------------------------------------------------------------===//
145
merge(const LinkerConfig & pConfig,const Input & pInput,TagType pTag,const ELFAttributeValue & pInAttr)146 bool ARMELFAttributeData::merge(const LinkerConfig& pConfig,
147 const Input &pInput, TagType pTag,
148 const ELFAttributeValue& pInAttr)
149 {
150 // Pre-condition
151 // 1. The out_attr must be initailized and has value of the same type as
152 // pInAttr.
153 // 2. The value helf by out_attr and pInAttr must be different.
154 ELFAttributeValue &out_attr = m_Attrs[pTag];
155
156 // Attribute in the output must have value assigned.
157 assert(out_attr.isInitialized() && "No output attribute to be merged!");
158
159 switch (pTag) {
160 case Tag_CPU_arch: {
161 // Need value of Tag_also_compatible_with in the input for merge.
162 if (pInAttr.getIntValue() <= CPU_Arch_Max) {
163 m_CPUArch = pInAttr.getIntValue();
164 } else {
165 error(diag::error_unknown_cpu_arch) << pInput.name();
166 return false;
167 }
168 break;
169 }
170 case Tag_CPU_name: {
171 // need value of Tag_CPU_arch in the input for merge
172 m_CPUName = pInAttr.getStringValue();
173 break;
174 }
175 case Tag_CPU_raw_name: {
176 // need value of Tag_CPU_arch in the input for merge
177 m_CPURawName = pInAttr.getStringValue();
178 break;
179 }
180 case Tag_FP_arch: {
181 // need value of Tag_HardFP_use in the input for merge
182 m_FPArch = pInAttr.getIntValue();
183 break;
184 }
185 case Tag_ABI_HardFP_use: {
186 // need value of Tag_FP_arch in the input for merge
187 m_HardFPUse = pInAttr.getIntValue();
188 break;
189 }
190 case Tag_also_compatible_with: {
191 // need value of Tag_CPU_arch in the input for merge
192 m_SecondaryCPUArch = decode_secondary_compatibility_attribute(pInAttr);
193 break;
194 }
195 case Tag_ABI_VFP_args: {
196 // need value of Tag_ABI_FP_number_model in the input for merge
197 m_VFPArgs = pInAttr.getIntValue();
198 break;
199 }
200 // The value of these tags are integers and after merge, only the greatest
201 // value held by pInAttr and out_attr goes into output.
202 case Tag_ARM_ISA_use:
203 case Tag_THUMB_ISA_use:
204 case Tag_WMMX_arch:
205 case Tag_Advanced_SIMD_arch:
206 case Tag_ABI_FP_rounding:
207 case Tag_ABI_FP_exceptions:
208 case Tag_ABI_FP_user_exceptions:
209 case Tag_ABI_FP_number_model:
210 case Tag_FP_HP_extension:
211 case Tag_CPU_unaligned_access:
212 case Tag_T2EE_use: {
213 assert((out_attr.type() == ELFAttributeValue::Int) &&
214 (pInAttr.type() == ELFAttributeValue::Int) &&
215 "should have integer parameeter!");
216 if (pInAttr.getIntValue() > out_attr.getIntValue())
217 out_attr.setIntValue(pInAttr.getIntValue());
218 break;
219 }
220 // The value of these tags are integers and after merge, only the smallest
221 // value held by pInAttr and out_attr goes into output.
222 case Tag_ABI_align_preserved:
223 case Tag_ABI_PCS_RO_data: {
224 assert((out_attr.type() == ELFAttributeValue::Int) &&
225 (pInAttr.type() == ELFAttributeValue::Int) &&
226 "should have integer parameeter!");
227 if (pInAttr.getIntValue() < out_attr.getIntValue())
228 out_attr.setIntValue(pInAttr.getIntValue());
229 break;
230 }
231 // The values of these attributes are sorted as 1 > 2 > 0. And the greater
232 // value becomes output.
233 case Tag_ABI_align_needed:
234 case Tag_ABI_FP_denormal:
235 case Tag_ABI_PCS_GOT_use: {
236 const int in_val = pInAttr.getIntValue();
237 const int out_val = out_attr.getIntValue();
238
239 if (in_val <= 2) {
240 if (out_val <= 2) {
241 // Use value_ordering_120 to determine the ordering.
242 if (value_ordering_120[in_val] > value_ordering_120[out_val]) {
243 out_attr.setIntValue(in_val);
244 }
245 }
246 } else {
247 // input value > 2, for future-proofing
248 if (in_val > out_val) {
249 out_attr.setIntValue(in_val);
250 }
251 }
252 break;
253 }
254 // These tags use the first value ever seen.
255 case Tag_ABI_optimization_goals:
256 case Tag_ABI_FP_optimization_goals: {
257 break;
258 }
259 // Tag_CPU_arch_profile
260 case Tag_CPU_arch_profile: {
261 if (pInAttr.getIntValue() == Arch_Profile_None)
262 return true;
263
264 switch (out_attr.getIntValue()) {
265 case Arch_Profile_None: {
266 out_attr.setIntValue(pInAttr.getIntValue());
267 break;
268 }
269 case Arch_Profile_RealOrApp: {
270 if (pInAttr.getIntValue() != Arch_Profile_Microcontroller)
271 out_attr.setIntValue(pInAttr.getIntValue());
272 else
273 warning(diag::warn_mismatch_cpu_arch_profile)
274 << pInAttr.getIntValue() << pInput.name();
275 break;
276 }
277 default: {
278 // out_attr is Arch_Profile_Application or Arch_Profile_Realtime or
279 // Arch_Profile_Microcontroller.
280 if ((pInAttr.getIntValue() == Arch_Profile_RealOrApp) &&
281 (out_attr.getIntValue() != Arch_Profile_Microcontroller)) {
282 // do nothing
283 } else {
284 if (pConfig.options().warnMismatch())
285 warning(diag::warn_mismatch_cpu_arch_profile)
286 << pInAttr.getIntValue() << pInput.name();
287 }
288 break;
289 }
290 }
291 break;
292 }
293 // Tag_MPextension_use and Tag_MPextension_use_legacy
294 case Tag_MPextension_use:
295 case Tag_MPextension_use_legacy: {
296 if (m_MPextensionUse < 0) {
297 m_MPextensionUse = pInAttr.getIntValue();
298 } else {
299 if (static_cast<unsigned>(m_MPextensionUse) != pInAttr.getIntValue()) {
300 warning(diag::error_mismatch_mpextension_use) << pInput.name();
301 }
302 }
303 break;
304 }
305 // Tag_DIV_use
306 case Tag_DIV_use: {
307 if (pInAttr.getIntValue() == 2) {
308 // 2 means the code was permitted to use SDIV/UDIV in anyway.
309 out_attr.setIntValue(2);
310 } else {
311 // Merge until settling down Tag_CPU_arch.
312 m_DIVUse = pInAttr.getIntValue();
313 }
314 break;
315 }
316 // Tag_ABI_enum_size
317 case Tag_ABI_enum_size: {
318 if ((out_attr.getIntValue() == Enum_Unused) ||
319 (out_attr.getIntValue() == Enum_Containerized_As_Possible))
320 out_attr.setIntValue(pInAttr.getIntValue());
321 else if (pInAttr.getIntValue() != Enum_Containerized_As_Possible &&
322 pConfig.options().warnMismatch())
323 warning(diag::warn_mismatch_enum_size)
324 << pInput.name() << pInAttr.getIntValue()
325 << out_attr.getIntValue();
326 break;
327 }
328 // Tag_ABI_FP_16bit_format
329 case Tag_ABI_FP_16bit_format: {
330 // 0: doesn't use any 16-bit FP number
331 // 1: use IEEE 754 format 16-bit FP number
332 // 2: use VFPv3/Advanced SIMD "alternative format" 16-bit FP number
333 if (pInAttr.getIntValue() != 0) {
334 if (out_attr.getIntValue() == 0) {
335 out_attr.setIntValue(pInAttr.getIntValue());
336 } else {
337 if (pConfig.options().warnMismatch())
338 warning(diag::warn_mismatch_fp16_format) << pInput.name();
339 }
340 }
341 break;
342 }
343 // Tag_nodefaults
344 case Tag_nodefaults: {
345 // There's nothing to do for this tag. It doesn't have an actual value.
346 break;
347 }
348 // Tag_conformance
349 case Tag_conformance: {
350 // Throw away the value if the attribute value doesn't match.
351 if (out_attr.getStringValue() != pInAttr.getStringValue())
352 out_attr.setStringValue("");
353 break;
354 }
355 // Tag_Virtualization_use
356 case Tag_Virtualization_use: {
357 // 0: No use of any virtualization extension
358 // 1: TrustZone
359 // 2: Virtualization extension such as HVC and ERET
360 // 3: TrustZone and virtualization extension are permitted
361 if (pInAttr.getIntValue() != 0) {
362 if (out_attr.getIntValue() == 0) {
363 out_attr.setIntValue(pInAttr.getIntValue());
364 } else {
365 if ((out_attr.getIntValue() <= 3) && (pInAttr.getIntValue() <= 3)) {
366 // Promote to 3
367 out_attr.setIntValue(3);
368 } else {
369 warning(diag::warn_unrecognized_virtualization_use)
370 << pInput.name() << pInAttr.getIntValue();
371 }
372 }
373 }
374 break;
375 }
376 // Tag_ABI_WMMX_args
377 case Tag_ABI_WMMX_args: {
378 // There's no way to merge this value (i.e., objects contain different
379 // value in this tag are definitely incompatible.)
380 if (pConfig.options().warnMismatch())
381 warning(diag::warn_mismatch_abi_wmmx_args) << pInput.name();
382 break;
383 }
384 // Tag_PCS_config
385 case Tag_PCS_config: {
386 // 0 means no standard configuration used or no information recorded.
387 if (pInAttr.getIntValue() != 0) {
388 if (out_attr.getIntValue() == 0)
389 out_attr.setIntValue(pInAttr.getIntValue());
390 else {
391 // Different values in these attribute are conflict
392 if (pConfig.options().warnMismatch())
393 warning(diag::warn_mismatch_pcs_config) << pInput.name();
394 }
395 }
396 break;
397 }
398 // Tag_ABI_PCS_R9_use
399 case Tag_ABI_PCS_R9_use: {
400 if (pInAttr.getIntValue() != R9_Unused) {
401 if (out_attr.getIntValue() == R9_Unused)
402 out_attr.setIntValue(pInAttr.getIntValue());
403 else {
404 if (pConfig.options().warnMismatch())
405 warning(diag::warn_mismatch_r9_use) << pInput.name();
406 }
407 }
408 break;
409 }
410 // Tag_ABI_PCS_RW_data
411 case Tag_ABI_PCS_RW_data: {
412 if (pInAttr.getIntValue() == RW_data_SB_Relative) {
413 // Require using R9 as SB (global Static Base register).
414 if ((out_attr.getIntValue() != R9_Unused) &&
415 (out_attr.getIntValue() != R9_SB) &&
416 pConfig.options().warnMismatch())
417 warning(diag::warn_mismatch_r9_use) << pInput.name();
418 }
419 // Choose the smaller value
420 if (pInAttr.getIntValue() < out_attr.getIntValue())
421 out_attr.setIntValue(pInAttr.getIntValue());
422 break;
423 }
424 // Tag_ABI_PCS_wchar_t
425 case Tag_ABI_PCS_wchar_t: {
426 // 0: no use of wchar_t
427 // 2: sizeof(wchar_t) = 2
428 // 4: sizeof(wchar_t) = 4
429 if (pInAttr.getIntValue() != 0) {
430 if (out_attr.getIntValue() == 0)
431 out_attr.setIntValue(pInAttr.getIntValue());
432 else {
433 if (pConfig.options().warnMismatch())
434 warning(diag::warn_mismatch_wchar_size)
435 << pInput.name() << pInAttr.getIntValue()
436 << out_attr.getIntValue();
437 }
438 }
439 break;
440 }
441 default: {
442 // Handle unknown attributes:
443 //
444 // Since we don't know how to merge the value of unknown attribute, we
445 // have to ignore it. There're two rules related to the processing (See
446 // ARM [ABI-addenda] 2.2.6, Coding extensibility and compatibility.):
447 //
448 // 1. For tag N where N >= 128, tag N has the same properties as
449 // tag N % 128.
450 // 2. Tag 64-127 can be safely ignored.
451 // 3. Tag 0-63 must be comprehended, therefore we cannot ignore.
452 if (pConfig.options().warnMismatch()) {
453 if ((pTag & 127) < 64) {
454 warning(diag::warn_unknown_mandatory_attribute) << pTag
455 << pInput.name();
456 } else {
457 warning(diag::warn_unknown_attribute) << pTag << pInput.name();
458 }
459 }
460 break;
461 }
462 }
463 return true;
464 }
465
466 //===--------------------------------------------------------------------===//
467 // Helper Functions for postMerge()
468 //===--------------------------------------------------------------------===//
469
470 namespace {
471
472 /*
473 * Helper function to encode value in Tag_also_compatible_with.
474 *
475 * @ref ARM [ABI-addenda], 2.3.7.3
476 */
477 static void
encode_secondary_compatibility_attribute(ELFAttributeValue & pValue,int pArch)478 encode_secondary_compatibility_attribute(ELFAttributeValue &pValue, int pArch)
479 {
480 if ((pArch < 0) || (pArch > ARMELFAttributeData::CPU_Arch_Max)) {
481 pValue.setStringValue("");
482 } else {
483 char new_value[] = {
484 ARMELFAttributeData::Tag_CPU_arch,
485 static_cast<char>(pArch),
486 0
487 };
488 pValue.setStringValue(std::string(new_value, sizeof(new_value)));
489 }
490 return;
491 }
492
493 /*
494 * Combine the main and secondary CPU arch value
495 */
496 static int
calculate_cpu_arch(int cpu_arch,int secondary_arch)497 calculate_cpu_arch(int cpu_arch, int secondary_arch)
498 {
499 // short-circuit
500 if ((secondary_arch < 0) ||
501 ((cpu_arch + secondary_arch) != (ARMELFAttributeData::CPU_Arch_ARM_V4T +
502 ARMELFAttributeData::CPU_Arch_ARM_V6_M)))
503 return cpu_arch;
504
505 if ((cpu_arch == ARMELFAttributeData::CPU_Arch_ARM_V4T) &&
506 (secondary_arch == ARMELFAttributeData::CPU_Arch_ARM_V6_M))
507 return ARMELFAttributeData::CPU_Arch_ARM_V4T_Plus_V6_M;
508 else if ((cpu_arch == ARMELFAttributeData::CPU_Arch_ARM_V6_M) &&
509 (secondary_arch == ARMELFAttributeData::CPU_Arch_ARM_V4T))
510 return ARMELFAttributeData::CPU_Arch_ARM_V4T_Plus_V6_M;
511 else
512 return cpu_arch;
513 }
514
515 /*
516 * Given a CPU arch X and a CPU arch Y in which Y is newer than X, the value in
517 * cpu_compatibility_table[X][Y] is the CPU arch required to run ISA both from X
518 * and Y. 0 in the table means unreachable and -1 means conflict architecture
519 * profile.
520 */
521 #define CPU(C) ARMELFAttributeData::CPU_Arch_ARM_ ## C
522 static const int cpu_compatibility_table[][CPU(V4T_Plus_V6_M) + 1] =
523 {
524 /* old\new ARM v6T2 ARM v6K ARM v7 ARM v6-M ARM v6S-M ARM v7E-M ARMv8, ARM v4t + v6-M */
525 /* Pre v4 */ { CPU(V6T2), CPU(V6K), CPU(V7), -1, -1, -1, -1, -1 },
526 /* ARM v4 */ { CPU(V6T2), CPU(V6K), CPU(V7), -1, -1, -1, -1, -1 },
527 /* ARM v4T */ { CPU(V6T2), CPU(V6K), CPU(V7), CPU(V6K), CPU(V6K), CPU(V7E_M), CPU(V8), CPU(V4T) },
528 /* ARM v5T */ { CPU(V6T2), CPU(V6K), CPU(V7), CPU(V6K), CPU(V6K), CPU(V7E_M), CPU(V8), CPU(V5T) },
529 /* ARM v5TE */ { CPU(V6T2), CPU(V6K), CPU(V7), CPU(V6K), CPU(V6K), CPU(V7E_M), CPU(V8), CPU(V5TE) },
530 /* ARM v5TEJ */ { CPU(V6T2), CPU(V6K), CPU(V7), CPU(V6K), CPU(V6K), CPU(V7E_M), CPU(V8), CPU(V5TEJ) },
531 /* ARM v6 */ { CPU(V6T2), CPU(V6K), CPU(V7), CPU(V6K), CPU(V6K), CPU(V7E_M), CPU(V8), CPU(V6) },
532 /* ARM v6KZ */ { CPU(V7), CPU(V6KZ), CPU(V7), CPU(V6KZ), CPU(V6KZ), CPU(V7E_M), CPU(V8), CPU(V6KZ) },
533 /* ARM v6T2 */ { CPU(V6T2), CPU(V7), CPU(V7), CPU(V7), CPU(V7), CPU(V7E_M), CPU(V8), CPU(V6T2) },
534 /* ARM v6K */ { 0, CPU(V6K), CPU(V7), CPU(V6K), CPU(V6K), CPU(V7E_M), CPU(V8), CPU(V6K) },
535 /* ARM v7 */ { 0, 0, CPU(V7), CPU(V7), CPU(V7), CPU(V7E_M), CPU(V8), CPU(V7) },
536 /* ARM v6-M */ { 0, 0, 0, CPU(V6_M), CPU(V6S_M), CPU(V7E_M), CPU(V8), CPU(V6_M) },
537 /* ARM v6S-M */ { 0, 0, 0, 0, CPU(V6S_M), CPU(V7E_M), CPU(V8), CPU(V6S_M) },
538 /* ARM v7E-M */ { 0, 0, 0, 0, 0, CPU(V7E_M), CPU(V8), CPU(V7E_M) },
539 /* ARM v8 */ { 0, 0, 0, 0, 0, 0, CPU(V8), CPU(V8) },
540 /* v4T + v6-M */ { 0, 0, 0, 0, 0, 0, 0, CPU(V4T_Plus_V6_M) }
541 };
542
543 /*
544 * Helper function to determine the merge of two different CPU arch.
545 */
merge_cpu_arch(int out_cpu_arch,int in_cpu_arch)546 static int merge_cpu_arch(int out_cpu_arch, int in_cpu_arch)
547 {
548 if (out_cpu_arch > CPU(V4T_Plus_V6_M))
549 return in_cpu_arch;
550
551 int new_cpu_arch, old_cpu_arch;
552 if (out_cpu_arch > in_cpu_arch) {
553 new_cpu_arch = out_cpu_arch;
554 old_cpu_arch = in_cpu_arch;
555 } else {
556 new_cpu_arch = in_cpu_arch;
557 old_cpu_arch = out_cpu_arch;
558 }
559
560 // No need to check the compatibility since the CPU architectures before
561 // V6KZ add features monotonically.
562 if (new_cpu_arch <= CPU(V6KZ))
563 return new_cpu_arch;
564
565 return cpu_compatibility_table[old_cpu_arch][new_cpu_arch - CPU(V6T2)];
566 }
567 #undef CPU
568
569 /*
570 * Generic CPU name is used when Tag_CPU_name is unable to guess during the
571 * merge of Tag_CPU_arch.
572 */
573 static const char* generic_cpu_name_table[] = {
574 /* Pre v4 */"Pre v4",
575 /* Pre v4 */"ARM v4",
576 /* ARM v4T */"ARM v4T",
577 /* ARM v5T */"ARM v5T",
578 /* ARM v5TE */"ARM v5TE",
579 /* ARM v5TEJ */"ARM v5TEJ",
580 /* ARM v6 */"ARM v6",
581 /* ARM v6KZ */"ARM v6KZ",
582 /* ARM v6T2 */"ARM v6T2",
583 /* ARM v6K */"ARM v6K",
584 /* ARM v7 */"ARM v7",
585 /* ARM v6-M */"ARM v6-M",
586 /* ARM v6S-M */"ARM v6S-M",
587 /* ARM v7E-M */"ARM v7E-M",
588 /* ARM v8 */"ARM v8",
589 };
590
get_generic_cpu_name(int cpu_arch)591 static const char* get_generic_cpu_name(int cpu_arch) {
592 assert(static_cast<size_t>(cpu_arch) <
593 (sizeof(generic_cpu_name_table) / sizeof(generic_cpu_name_table[0])));
594 return generic_cpu_name_table[cpu_arch];
595 }
596
597 /*
598 * Helper functions & data used in the merge of two different FP arch.
599 */
600 static const struct fp_config_data {
601 int version;
602 int regs;
603 } fp_configs[] = {
604 { 0, 0 },
605 { 1, 16 },
606 { 2, 16 },
607 { 3, 32 },
608 { 3, 16 },
609 { 4, 32 },
610 { 4, 16 },
611 { 8, 32 },
612 { 8, 16 },
613 };
614
615 static const size_t num_fp_configs =
616 sizeof(fp_configs) / sizeof(fp_config_data);
617
618 // Given h(x, y) = (x * (y >> 4) + (y >> 5))
619 //
620 // fp_config_hash_table[ h(0, 0) = 0 ] = 0
621 // fp_config_hash_table[ h(1, 16) = 1 ] = 1
622 // fp_config_hash_table[ h(2, 16) = 2 ] = 2
623 // fp_config_hash_table[ h(3, 32) = 7 ] = 3
624 // fp_config_hash_table[ h(3, 16) = 3 ] = 4
625 // fp_config_hash_table[ h(4, 32) = 9 ] = 5
626 // fp_config_hash_table[ h(4, 16) = 4 ] = 6
627 // fp_config_hash_table[ h(8, 32) = 17 ] = 7
628 // fp_config_hash_table[ h(8, 16) = 8 ] = 8
629 //
630 // h(0, 0) = 0
631 static const uint8_t fp_config_hash_table[] =
632 {
633 #define UND static_cast<uint8_t>(-1)
634 /* 0 */0,
635 /* 1 */1,
636 /* 2 */2,
637 /* 3 */4,
638 /* 4 */6,
639 /* 5 */UND,
640 /* 6 */UND,
641 /* 7 */3,
642 /* 8 */8,
643 /* 9 */5,
644 /* 10 */UND,
645 /* 11 */UND,
646 /* 12 */UND,
647 /* 13 */UND,
648 /* 14 */UND,
649 /* 15 */UND,
650 /* 16 */UND,
651 /* 17 */7,
652 #undef UND
653 };
654
655 static const size_t num_hash_table_entries =
656 sizeof(fp_config_hash_table) / sizeof(fp_config_hash_table[0]);
657
calculate_fp_config_hash(const struct fp_config_data & pConfig)658 static int calculate_fp_config_hash(const struct fp_config_data &pConfig)
659 {
660 int x = pConfig.version;
661 int y = pConfig.regs;
662 return (x * (y >> 4) + (y >> 5));
663 }
664
get_fp_arch_of_config(const struct fp_config_data & pConfig)665 static int get_fp_arch_of_config(const struct fp_config_data &pConfig)
666 {
667 int hash = calculate_fp_config_hash(pConfig);
668 assert(static_cast<size_t>(hash) < num_hash_table_entries);
669 return fp_config_hash_table[hash];
670 }
671
is_allowed_use_of_div(int cpu_arch,int cpu_arch_profile,int div_use)672 static bool is_allowed_use_of_div(int cpu_arch, int cpu_arch_profile,
673 int div_use) {
674 // 0: The code was permitted to use SDIV and UDIV in the Thumb ISA on v7-R or
675 // v7-M.
676 // 1: The code was not permitted to use SDIV and UDIV.
677 // 2: The code was explicitly permitted to use SDIV and UDIV.
678 switch (div_use) {
679 case 0: {
680 if ((cpu_arch == ARMELFAttributeData::CPU_Arch_ARM_V7) &&
681 ((cpu_arch_profile == 'R') || (cpu_arch_profile == 'M'))) {
682 return true;
683 } else {
684 return (cpu_arch >= ARMELFAttributeData::CPU_Arch_ARM_V7E_M);
685 }
686 }
687 case 1: {
688 return false;
689 }
690 case 2:
691 // For future proofing
692 default: {
693 return true;
694 }
695 }
696 }
697
698 } // anonymous namespace
699
700 //===--------------------------------------------------------------------===//
701 // End Helper Functions for postMerge()
702 //===--------------------------------------------------------------------===//
703
postMerge(const LinkerConfig & pConfig,const Input & pInput)704 bool ARMELFAttributeData::postMerge(const LinkerConfig& pConfig,
705 const Input &pInput)
706 {
707 // Process Tag_CPU_arch, Tag_CPU_name, Tag_CPU_raw_name, and
708 // Tag_also_compatible_with.
709 ELFAttributeValue &out_cpu_arch_attr = m_Attrs[Tag_CPU_arch];
710 ELFAttributeValue &out_secondary_compatibility_attr =
711 m_Attrs[Tag_also_compatible_with];
712
713 if ((m_CurrentCPUArch < 0) && out_cpu_arch_attr.isInitialized()) {
714 // Current input initializes the value of Tag_CPU_arch. Validate it.
715 int out_cpu_arch = out_cpu_arch_attr.getIntValue();
716
717 if (out_cpu_arch > CPU_Arch_Max) {
718 error(diag::error_unknown_cpu_arch) << pInput.name();
719 return false;
720 }
721
722 // Initialize m_CurrentCPUArch.
723 int out_secondary_arch = -1;
724 if (out_secondary_compatibility_attr.isInitialized())
725 out_secondary_arch = decode_secondary_compatibility_attribute(
726 out_secondary_compatibility_attr);
727
728 m_CurrentCPUArch = calculate_cpu_arch(out_cpu_arch, out_secondary_arch);
729 }
730
731 if (m_CPUArch >= 0) {
732 assert(out_cpu_arch_attr.isInitialized() && "CPU arch has never set!");
733 assert(m_CurrentCPUArch >= 0);
734
735 int in_cpu_arch = calculate_cpu_arch(m_CPUArch, m_SecondaryCPUArch);
736 int result_cpu_arch = merge_cpu_arch(m_CurrentCPUArch, in_cpu_arch);
737
738 if (result_cpu_arch < 0) {
739 warning(diag::warn_mismatch_cpu_arch_profile)
740 << in_cpu_arch << pInput.name();
741 } else {
742 if (result_cpu_arch != m_CurrentCPUArch) {
743 // Value of Tag_CPU_arch are going to changea.
744 m_CurrentCPUArch = result_cpu_arch;
745
746 // Write the result value to the output.
747 if (result_cpu_arch == CPU_Arch_ARM_V4T_Plus_V6_M) {
748 out_cpu_arch_attr.setIntValue(CPU_Arch_ARM_V4T);
749 encode_secondary_compatibility_attribute(
750 out_secondary_compatibility_attr, CPU_Arch_ARM_V6_M);
751 } else {
752 out_cpu_arch_attr.setIntValue(result_cpu_arch);
753 encode_secondary_compatibility_attribute(
754 out_secondary_compatibility_attr, -1);
755 }
756
757 ELFAttributeValue &out_cpu_name = m_Attrs[Tag_CPU_name];
758 ELFAttributeValue &out_cpu_raw_name = m_Attrs[Tag_CPU_raw_name];
759
760 if (m_CurrentCPUArch != in_cpu_arch) {
761 // Unable to guess the Tag_CPU_name. Use the generic name.
762 if (out_cpu_name.isInitialized()) {
763 out_cpu_name.setStringValue(get_generic_cpu_name(m_CurrentCPUArch));
764 }
765
766 // Tag_CPU_raw_name becomes unknown. Set to default value to disable
767 // it.
768 out_cpu_raw_name.setStringValue("");
769 } else {
770 // Use the value of Tag_CPU_name and Tag_CPU_raw_name from the input.
771 if (!m_CPUName.empty()) {
772 ELFAttributeValue &out_cpu_name = m_Attrs[Tag_CPU_name];
773 assert(out_cpu_name.isInitialized() && "CPU name has never set!");
774 out_cpu_name.setStringValue(m_CPUName);
775 }
776
777 if (!m_CPURawName.empty()) {
778 ELFAttributeValue &out_cpu_raw_name = m_Attrs[Tag_CPU_raw_name];
779 assert(out_cpu_raw_name.isInitialized() &&
780 "CPU raw name has never set!");
781 out_cpu_raw_name.setStringValue(m_CPURawName);
782 }
783 }
784 }
785 }
786 } // (m_CPUArch >= 0)
787
788 // Process Tag_ABI_VFP_args.
789 if (m_VFPArgs >= 0) {
790 ELFAttributeValue &out_attr = m_Attrs[Tag_ABI_VFP_args];
791 ELFAttributeValue &out_float_number_model_attr =
792 m_Attrs[Tag_ABI_FP_number_model];
793
794 assert(out_attr.isInitialized() && "VFP args has never set!");
795
796 // If the output is not permitted to use floating number, this attribute
797 // is ignored (migrate the value from input directly.)
798 if (out_float_number_model_attr.isUninitialized() ||
799 (out_float_number_model_attr.getIntValue() == 0)) {
800 // Inherit requirement from input.
801 out_attr.setIntValue(m_VFPArgs);
802 } else {
803 if (pConfig.options().warnMismatch())
804 warning(diag::warn_mismatch_vfp_args) << pInput.name();
805 }
806 }
807
808 // Process Tag_FP_arch.
809 ELFAttributeValue &out_fp_arch_attr = m_Attrs[Tag_FP_arch];
810 if (m_FPArch >= 0) {
811 assert(out_fp_arch_attr.isInitialized() && "FP arch has never set!");
812
813 // Tag_FP_arch
814 // 0: instructions requiring FP hardware are not permitted
815 // 1: VFP1
816 // 2: VFP2
817 // 3: VFP3 D32
818 // 4: VFP3 D16
819 // 5: VFP4 D32
820 // 6: VFP4 D16
821 // 7: ARM v8-A D32
822 // 8: ARM v8-A D16
823 if (out_fp_arch_attr.getIntValue() == 0) {
824 // Output has no constraints on FP hardware. Copy the requirement from
825 // input.
826 out_fp_arch_attr.setIntValue(m_FPArch);
827 } else if (m_FPArch == 0) {
828 // Input has no constraints on FP hardware. Do nothing.
829 } else {
830 // If here, both output and input contain non-zero value of Tag_FP_arch.
831
832 // Version greater than num_fp_configs is not defined. Choose the greater
833 // one for future-proofing.
834 if (static_cast<unsigned>(m_FPArch) > num_fp_configs) {
835 if (static_cast<unsigned>(m_FPArch) > out_fp_arch_attr.getIntValue()) {
836 out_fp_arch_attr.setIntValue(m_FPArch);
837 }
838 } else {
839 if (out_fp_arch_attr.getIntValue() < num_fp_configs) {
840 const struct fp_config_data &input_fp_config = fp_configs[ m_FPArch ];
841
842 const struct fp_config_data &output_fp_config =
843 fp_configs[ out_fp_arch_attr.getIntValue() ];
844
845 const struct fp_config_data result_fp_config = {
846 /*version*/((output_fp_config.version > input_fp_config.version) ?
847 output_fp_config.version : input_fp_config.version),
848 /* regs */((output_fp_config.regs > input_fp_config.regs) ?
849 output_fp_config.regs : input_fp_config.regs),
850 };
851 // Find the attribute value corresponding the result_fp_config
852 out_fp_arch_attr.setIntValue(get_fp_arch_of_config(result_fp_config));
853 }
854 }
855 }
856 } // (m_FPArch >= 0)
857
858 // Process Tag_ABI_HardFP_use.
859 ELFAttributeValue &out_hardfp_use_attr = m_Attrs[Tag_ABI_HardFP_use];
860
861 if (!m_HardFPUseInitialized && out_hardfp_use_attr.isInitialized()) {
862 m_HardFPUse = out_hardfp_use_attr.getIntValue();
863 m_HardFPUseInitialized = true;
864 }
865
866 if (m_HardFPUse >= 0) {
867 // Tag_ABI_HardFP_use depends on the meaning of Tag_FP_arch when it's 0.
868 assert(out_hardfp_use_attr.isInitialized() && "HardFP use has never set!");
869
870 if (out_fp_arch_attr.isUninitialized() ||
871 (out_fp_arch_attr.getIntValue() == 0)) {
872 // Has no constraints on FP hardware.
873 out_hardfp_use_attr.setIntValue(m_HardFPUse);
874 } else {
875 // Both output and input contain non-zero value of Tag_FP_arch and we have
876 // different Tag_ABI_HaedFP_Use settings other than 0.
877 if ((out_fp_arch_attr.getIntValue() > 0) && (m_HardFPUse > 0))
878 // Promote to 3 (The user permitted this entity to use both SP and DP
879 // VFP instruction.)
880 out_hardfp_use_attr.setIntValue(3);
881 }
882 }
883
884 // Move the value of Tag_MPextension_use_legacy to Tag_MPextension_use.
885 ELFAttributeValue &out_mpextension_use_legacy =
886 m_Attrs[Tag_MPextension_use_legacy];
887
888 ELFAttributeValue &out_mpextension_use = m_Attrs[Tag_MPextension_use];
889
890 // If Tag_MPextension_use_legacy has value, it must be introduced by current
891 // input since it is reset every time after the merge completed.
892 if (out_mpextension_use_legacy.isInitialized()) {
893 if (out_mpextension_use.isInitialized()) {
894 if (m_MPextensionUse < 0) {
895 // The value of Tag_MPextension_use is introduced by the current input.
896 // Check whether it is consistent with the one set in legacy.
897 m_MPextensionUse = out_mpextension_use.getIntValue();
898 } else {
899 // Current input introduces value of Tag_MPextension_use in
900 // m_MPextensionUse.
901 }
902
903 // Check the consistency between m_MPextensionUse and the value of
904 // Tag_MPextension_use_legacy.
905 if (static_cast<unsigned>(m_MPextensionUse) !=
906 out_mpextension_use_legacy.getIntValue()) {
907 error(diag::error_mismatch_mpextension_use) << pInput.name();
908 return false;
909 }
910 } else {
911 if (m_MPextensionUse < 0) {
912 // Tag_MPextension_use is not set. Initialize it and move the value.
913 out_mpextension_use.setType(ELFAttributeValue::Int);
914 out_mpextension_use.setIntValue(out_mpextension_use.getIntValue());
915 } else {
916 // Unreachable case since the value to unitialized attribute is directly
917 // assigned in ELFAttribute::Subsection::merge().
918 assert(false && "Tag_MPextension_use is uninitialized but have value?");
919 }
920 }
921
922 // Reset the attribute to uninitialized so it won't be included in the
923 // output.
924 out_mpextension_use_legacy.setType(ELFAttributeValue::Uninitialized);
925 }
926
927 // Process Tag_MPextension_use.
928 if (m_MPextensionUse > 0) {
929 assert(out_mpextension_use.isInitialized());
930
931 if (static_cast<unsigned>(m_MPextensionUse) >
932 out_mpextension_use.getIntValue()) {
933 out_mpextension_use.setIntValue(m_MPextensionUse);
934 }
935 }
936
937 // Process Tag_DIV_use.
938 ELFAttributeValue &out_div_use_attr = m_Attrs[Tag_DIV_use];
939
940 if (!m_DIVUseInitialized && out_div_use_attr.isInitialized()) {
941 // Perform the merge by reverting value of Tag_DIV_use and setup m_DIVUse.
942 m_DIVUse = out_div_use_attr.getIntValue();
943 out_div_use_attr.setIntValue(0);
944 m_DIVUseInitialized = true;
945 }
946
947 if (m_DIVUse >= 0) {
948 assert(out_div_use_attr.isInitialized());
949
950 const ELFAttributeValue &out_cpu_arch_profile_attr =
951 m_Attrs[Tag_CPU_arch_profile];
952
953 int out_cpu_arch_profile = Arch_Profile_None;
954 if (out_cpu_arch_profile_attr.isInitialized()) {
955 out_cpu_arch_profile = out_cpu_arch_profile_attr.getIntValue();
956 }
957
958 if (m_DIVUse == 1) {
959 // Input (=1) was not permitted to use SDIV and UDIV. See whether current
960 // output was explicitly permitted the use.
961 if (!is_allowed_use_of_div(m_CurrentCPUArch, out_cpu_arch_profile,
962 out_div_use_attr.getIntValue())) {
963 out_div_use_attr.setIntValue(1);
964 }
965 } else {
966 if (out_div_use_attr.getIntValue() != 1) {
967 // Output does not explicitly forbid the use of SDIV/UDIV. See whether
968 // the input attribute can allow it under current CPU architecture
969 // profile.
970 if (is_allowed_use_of_div(m_CurrentCPUArch, out_cpu_arch_profile,
971 m_DIVUse)) {
972 out_div_use_attr.setIntValue(m_DIVUse);
973 }
974 }
975 }
976 }
977
978 return true;
979 }
980
sizeOutput() const981 size_t ARMELFAttributeData::sizeOutput() const {
982 size_t result = 0;
983
984 // Size contributed by known attributes
985 for (unsigned i = 0; i <= Tag_Max; ++i) {
986 TagType tag = static_cast<TagType>(i);
987 const ELFAttributeValue &value = m_Attrs[tag];
988
989 if (value.shouldEmit()) {
990 result += leb128::size(static_cast<uint32_t>(tag));
991 result += value.getSize();
992 }
993 }
994
995 // Size contributed by unknown attributes
996 for (UnknownAttrsMap::const_iterator unknown_attr_it = m_UnknownAttrs.begin(),
997 unknown_attr_end = m_UnknownAttrs.end();
998 unknown_attr_it != unknown_attr_end; ++unknown_attr_it) {
999 TagType tag = unknown_attr_it->first;
1000 const ELFAttributeValue &value = unknown_attr_it->second;
1001
1002 if (value.shouldEmit()) {
1003 result += leb128::size(static_cast<uint32_t>(tag));
1004 result += value.getSize();
1005 }
1006 }
1007
1008 return result;
1009 }
1010
emit(char * pBuf) const1011 size_t ARMELFAttributeData::emit(char *pBuf) const {
1012 char *buffer = pBuf;
1013
1014 // Tag_conformance "should be emitted first in a file-scope sub-subsection of
1015 // the first public subsection of the attribute section."
1016 //
1017 // See ARM [ABI-addenda], 2.3.7.4 Conformance tag
1018 const ELFAttributeValue &attr_conformance = m_Attrs[Tag_conformance];
1019
1020 if (attr_conformance.shouldEmit()) {
1021 if (!ELFAttributeData::WriteAttribute(Tag_conformance, attr_conformance,
1022 buffer)) {
1023 return 0;
1024 }
1025 }
1026
1027 // Tag_nodefaults "should be emitted before any other tag in an attribute
1028 // subsection other that the conformance tag"
1029 //
1030 // See ARM [ABI-addenda], 2.3.7.5 No defaults tag
1031 const ELFAttributeValue &attr_nodefaults = m_Attrs[Tag_nodefaults];
1032
1033 if (attr_nodefaults.shouldEmit()) {
1034 if (!ELFAttributeData::WriteAttribute(Tag_nodefaults, attr_nodefaults,
1035 buffer)) {
1036 return 0;
1037 }
1038 }
1039
1040 // Tag_conformance (=67)
1041 // Tag_nodefaults (=64)
1042 for (unsigned i = 0; i < Tag_nodefaults; ++i) {
1043 TagType tag = static_cast<TagType>(i);
1044 const ELFAttributeValue &value = m_Attrs[tag];
1045
1046 if (value.shouldEmit() &&
1047 !ELFAttributeData::WriteAttribute(tag, value, buffer)) {
1048 return 0;
1049 }
1050 }
1051
1052 for (unsigned i = (Tag_nodefaults + 1); i <= Tag_Max; ++i) {
1053 TagType tag = static_cast<TagType>(i);
1054 const ELFAttributeValue &value = m_Attrs[tag];
1055
1056 if (value.shouldEmit() && (i != Tag_conformance) &&
1057 !ELFAttributeData::WriteAttribute(tag, value, buffer)) {
1058 return 0;
1059 }
1060 }
1061
1062 for (UnknownAttrsMap::const_iterator unknown_attr_it = m_UnknownAttrs.begin(),
1063 unknown_attr_end = m_UnknownAttrs.end();
1064 unknown_attr_it != unknown_attr_end; ++unknown_attr_it) {
1065 TagType tag = unknown_attr_it->first;
1066 const ELFAttributeValue &value = unknown_attr_it->second;
1067
1068 if (value.shouldEmit() &&
1069 !ELFAttributeData::WriteAttribute(tag, value, buffer)) {
1070 return 0;
1071 }
1072 }
1073
1074 return (buffer - pBuf);
1075 }
1076
usingThumb() const1077 bool ARMELFAttributeData::usingThumb() const
1078 {
1079 int arch = m_Attrs[Tag_CPU_arch].getIntValue();
1080 if ((arch == CPU_Arch_ARM_V6_M) || (arch == CPU_Arch_ARM_V6S_M))
1081 return true;
1082 if ((arch != CPU_Arch_ARM_V7) && (arch != CPU_Arch_ARM_V7E_M))
1083 return false;
1084
1085 arch = m_Attrs[Tag_CPU_arch_profile].getIntValue();
1086 return arch == Arch_Profile_Microcontroller;
1087 }
1088
usingThumb2() const1089 bool ARMELFAttributeData::usingThumb2() const
1090 {
1091 int arch = m_Attrs[Tag_CPU_arch].getIntValue();
1092 return (arch == CPU_Arch_ARM_V6T2) || (arch == CPU_Arch_ARM_V7);
1093 }
1094