1 //===- GNULDBackend.cpp ---------------------------------------------------===//
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 <mcld/Target/GNULDBackend.h>
10
11 #include <string>
12 #include <cstring>
13 #include <cassert>
14 #include <vector>
15 #include <algorithm>
16 #include <map>
17
18 #include <mcld/Module.h>
19 #include <mcld/LinkerConfig.h>
20 #include <mcld/IRBuilder.h>
21 #include <mcld/InputTree.h>
22 #include <mcld/Config/Config.h>
23 #include <mcld/ADT/SizeTraits.h>
24 #include <mcld/LD/LDSymbol.h>
25 #include <mcld/LD/LDContext.h>
26 #include <mcld/Fragment/FillFragment.h>
27 #include <mcld/LD/EhFrame.h>
28 #include <mcld/LD/EhFrameHdr.h>
29 #include <mcld/LD/RelocData.h>
30 #include <mcld/LD/RelocationFactory.h>
31 #include <mcld/MC/Attribute.h>
32 #include <mcld/Support/MemoryArea.h>
33 #include <mcld/Support/MemoryRegion.h>
34 #include <mcld/Support/MsgHandling.h>
35 #include <mcld/Support/MemoryAreaFactory.h>
36 #include <mcld/LD/BranchIslandFactory.h>
37 #include <mcld/LD/StubFactory.h>
38 #include <mcld/Object/ObjectBuilder.h>
39
40 using namespace mcld;
41
42 //===--------------------------------------------------------------------===//
43 // non-member functions
44 //===----------------------------------------------------------------------===//
45
46 /// isCIdentifier - return if the pName is a valid C identifier
isCIdentifier(const std::string & pName)47 static bool isCIdentifier(const std::string& pName)
48 {
49 std::string ident = "0123456789"
50 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
51 "abcdefghijklmnopqrstuvwxyz"
52 "_";
53 return (pName.find_first_not_of(ident) > pName.length());
54 }
55
56 //===----------------------------------------------------------------------===//
57 // GNULDBackend
58 //===----------------------------------------------------------------------===//
GNULDBackend(const LinkerConfig & pConfig,GNUInfo * pInfo)59 GNULDBackend::GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo)
60 : TargetLDBackend(pConfig),
61 m_pObjectReader(NULL),
62 m_pDynObjFileFormat(NULL),
63 m_pExecFileFormat(NULL),
64 m_pObjectFileFormat(NULL),
65 m_pInfo(pInfo),
66 m_ELFSegmentTable(9), // magic number
67 m_pBRIslandFactory(NULL),
68 m_pStubFactory(NULL),
69 m_pEhFrameHdr(NULL),
70 m_bHasTextRel(false),
71 m_bHasStaticTLS(false),
72 f_pPreInitArrayStart(NULL),
73 f_pPreInitArrayEnd(NULL),
74 f_pInitArrayStart(NULL),
75 f_pInitArrayEnd(NULL),
76 f_pFiniArrayStart(NULL),
77 f_pFiniArrayEnd(NULL),
78 f_pStack(NULL),
79 f_pDynamic(NULL),
80 f_pTDATA(NULL),
81 f_pTBSS(NULL),
82 f_pExecutableStart(NULL),
83 f_pEText(NULL),
84 f_p_EText(NULL),
85 f_p__EText(NULL),
86 f_pEData(NULL),
87 f_p_EData(NULL),
88 f_pBSSStart(NULL),
89 f_pEnd(NULL),
90 f_p_End(NULL) {
91 m_pSymIndexMap = new HashTableType(1024);
92 }
93
~GNULDBackend()94 GNULDBackend::~GNULDBackend()
95 {
96 delete m_pInfo;
97 delete m_pDynObjFileFormat;
98 delete m_pExecFileFormat;
99 delete m_pObjectFileFormat;
100 delete m_pSymIndexMap;
101 delete m_pEhFrameHdr;
102 delete m_pBRIslandFactory;
103 delete m_pStubFactory;
104 }
105
sectionStartOffset() const106 size_t GNULDBackend::sectionStartOffset() const
107 {
108 if (LinkerConfig::Binary == config().codeGenType())
109 return 0x0;
110
111 switch (config().targets().bitclass()) {
112 case 32u:
113 return sizeof(llvm::ELF::Elf32_Ehdr) +
114 numOfSegments() * sizeof(llvm::ELF::Elf32_Phdr);
115 case 64u:
116 return sizeof(llvm::ELF::Elf64_Ehdr) +
117 numOfSegments() * sizeof(llvm::ELF::Elf64_Phdr);
118 default:
119 fatal(diag::unsupported_bitclass) << config().targets().triple().str()
120 << config().targets().bitclass();
121 return 0;
122 }
123 }
124
segmentStartAddr() const125 uint64_t GNULDBackend::segmentStartAddr() const
126 {
127 ScriptOptions::AddressMap::const_iterator mapping =
128 config().scripts().addressMap().find(".text");
129 if (mapping != config().scripts().addressMap().end())
130 return mapping.getEntry()->value();
131 else if (config().isCodeIndep())
132 return 0x0;
133 else
134 return m_pInfo->defaultTextSegmentAddr();
135 }
136
137 GNUArchiveReader*
createArchiveReader(Module & pModule)138 GNULDBackend::createArchiveReader(Module& pModule)
139 {
140 assert(NULL != m_pObjectReader);
141 return new GNUArchiveReader(pModule, *m_pObjectReader);
142 }
143
createObjectReader(IRBuilder & pBuilder)144 ELFObjectReader* GNULDBackend::createObjectReader(IRBuilder& pBuilder)
145 {
146 m_pObjectReader = new ELFObjectReader(*this, pBuilder, config());
147 return m_pObjectReader;
148 }
149
createDynObjReader(IRBuilder & pBuilder)150 ELFDynObjReader* GNULDBackend::createDynObjReader(IRBuilder& pBuilder)
151 {
152 return new ELFDynObjReader(*this, pBuilder, config());
153 }
154
createBinaryReader(IRBuilder & pBuilder)155 ELFBinaryReader* GNULDBackend::createBinaryReader(IRBuilder& pBuilder)
156 {
157 return new ELFBinaryReader(*this, pBuilder, config());
158 }
159
createWriter()160 ELFObjectWriter* GNULDBackend::createWriter()
161 {
162 return new ELFObjectWriter(*this, config());
163 }
164
initStdSections(ObjectBuilder & pBuilder)165 bool GNULDBackend::initStdSections(ObjectBuilder& pBuilder)
166 {
167 switch (config().codeGenType()) {
168 case LinkerConfig::DynObj: {
169 if (NULL == m_pDynObjFileFormat)
170 m_pDynObjFileFormat = new ELFDynObjFileFormat();
171 m_pDynObjFileFormat->initStdSections(pBuilder,
172 config().targets().bitclass());
173 return true;
174 }
175 case LinkerConfig::Exec:
176 case LinkerConfig::Binary: {
177 if (NULL == m_pExecFileFormat)
178 m_pExecFileFormat = new ELFExecFileFormat();
179 m_pExecFileFormat->initStdSections(pBuilder,
180 config().targets().bitclass());
181 return true;
182 }
183 case LinkerConfig::Object: {
184 if (NULL == m_pObjectFileFormat)
185 m_pObjectFileFormat = new ELFObjectFileFormat();
186 m_pObjectFileFormat->initStdSections(pBuilder,
187 config().targets().bitclass());
188 return true;
189 }
190 default:
191 fatal(diag::unrecognized_output_file) << config().codeGenType();
192 return false;
193 }
194 }
195
196 /// initStandardSymbols - define and initialize standard symbols.
197 /// This function is called after section merging but before read relocations.
initStandardSymbols(IRBuilder & pBuilder,Module & pModule)198 bool GNULDBackend::initStandardSymbols(IRBuilder& pBuilder,
199 Module& pModule)
200 {
201 if (LinkerConfig::Object == config().codeGenType())
202 return true;
203
204 // GNU extension: define __start and __stop symbols for the sections whose
205 // name can be presented as C symbol
206 // ref: GNU gold, Layout::define_section_symbols
207 Module::iterator iter, iterEnd = pModule.end();
208 for (iter = pModule.begin(); iter != iterEnd; ++iter) {
209 LDSection* section = *iter;
210
211 switch (section->kind()) {
212 case LDFileFormat::Relocation:
213 continue;
214 case LDFileFormat::EhFrame:
215 if (!section->hasEhFrame())
216 continue;
217 break;
218 default:
219 if (!section->hasSectionData())
220 continue;
221 break;
222 } // end of switch
223
224 if (isCIdentifier(section->name())) {
225 llvm::StringRef start_name = llvm::StringRef("__start_" + section->name());
226 FragmentRef* start_fragref = FragmentRef::Create(
227 section->getSectionData()->front(), 0x0);
228 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
229 start_name,
230 ResolveInfo::NoType,
231 ResolveInfo::Define,
232 ResolveInfo::Global,
233 0x0, // size
234 0x0, // value
235 start_fragref, // FragRef
236 ResolveInfo::Default);
237
238 llvm::StringRef stop_name = llvm::StringRef("__stop_" + section->name());
239 FragmentRef* stop_fragref = FragmentRef::Create(
240 section->getSectionData()->front(), section->size());
241 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
242 stop_name,
243 ResolveInfo::NoType,
244 ResolveInfo::Define,
245 ResolveInfo::Global,
246 0x0, // size
247 0x0, // value
248 stop_fragref, // FragRef
249 ResolveInfo::Default);
250 }
251 }
252
253 ELFFileFormat* file_format = getOutputFormat();
254
255 // ----- section symbols ----- //
256 // .preinit_array
257 FragmentRef* preinit_array = NULL;
258 if (file_format->hasPreInitArray()) {
259 preinit_array = FragmentRef::Create(
260 file_format->getPreInitArray().getSectionData()->front(),
261 0x0);
262 }
263 else {
264 preinit_array = FragmentRef::Null();
265 }
266 f_pPreInitArrayStart =
267 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
268 "__preinit_array_start",
269 ResolveInfo::NoType,
270 ResolveInfo::Define,
271 ResolveInfo::Global,
272 0x0, // size
273 0x0, // value
274 preinit_array, // FragRef
275 ResolveInfo::Hidden);
276 f_pPreInitArrayEnd =
277 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
278 "__preinit_array_end",
279 ResolveInfo::NoType,
280 ResolveInfo::Define,
281 ResolveInfo::Global,
282 0x0, // size
283 0x0, // value
284 FragmentRef::Null(), // FragRef
285 ResolveInfo::Hidden);
286
287 // .init_array
288 FragmentRef* init_array = NULL;
289 if (file_format->hasInitArray()) {
290 init_array = FragmentRef::Create(
291 file_format->getInitArray().getSectionData()->front(),
292 0x0);
293 }
294 else {
295 init_array = FragmentRef::Null();
296 }
297
298 f_pInitArrayStart =
299 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
300 "__init_array_start",
301 ResolveInfo::NoType,
302 ResolveInfo::Define,
303 ResolveInfo::Global,
304 0x0, // size
305 0x0, // value
306 init_array, // FragRef
307 ResolveInfo::Hidden);
308 f_pInitArrayEnd =
309 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
310 "__init_array_end",
311 ResolveInfo::NoType,
312 ResolveInfo::Define,
313 ResolveInfo::Global,
314 0x0, // size
315 0x0, // value
316 init_array, // FragRef
317 ResolveInfo::Hidden);
318
319 // .fini_array
320 FragmentRef* fini_array = NULL;
321 if (file_format->hasFiniArray()) {
322 fini_array = FragmentRef::Create(
323 file_format->getFiniArray().getSectionData()->front(),
324 0x0);
325 }
326 else {
327 fini_array = FragmentRef::Null();
328 }
329
330 f_pFiniArrayStart =
331 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
332 "__fini_array_start",
333 ResolveInfo::NoType,
334 ResolveInfo::Define,
335 ResolveInfo::Global,
336 0x0, // size
337 0x0, // value
338 fini_array, // FragRef
339 ResolveInfo::Hidden);
340 f_pFiniArrayEnd =
341 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
342 "__fini_array_end",
343 ResolveInfo::NoType,
344 ResolveInfo::Define,
345 ResolveInfo::Global,
346 0x0, // size
347 0x0, // value
348 fini_array, // FragRef
349 ResolveInfo::Hidden);
350
351 // .stack
352 FragmentRef* stack = NULL;
353 if (file_format->hasStack()) {
354 stack = FragmentRef::Create(
355 file_format->getStack().getSectionData()->front(),
356 0x0);
357 }
358 else {
359 stack = FragmentRef::Null();
360 }
361
362 f_pStack =
363 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
364 "__stack",
365 ResolveInfo::NoType,
366 ResolveInfo::Define,
367 ResolveInfo::Global,
368 0x0, // size
369 0x0, // value
370 stack, // FragRef
371 ResolveInfo::Hidden);
372
373 // _DYNAMIC
374 // TODO: add SectionData for .dynamic section, and then we can get the correct
375 // symbol section index for _DYNAMIC. Now it will be ABS.
376 f_pDynamic =
377 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
378 "_DYNAMIC",
379 ResolveInfo::Object,
380 ResolveInfo::Define,
381 ResolveInfo::Local,
382 0x0, // size
383 0x0, // value
384 FragmentRef::Null(), // FragRef
385 ResolveInfo::Hidden);
386
387 // ----- segment symbols ----- //
388 f_pExecutableStart =
389 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
390 "__executable_start",
391 ResolveInfo::NoType,
392 ResolveInfo::Define,
393 ResolveInfo::Absolute,
394 0x0, // size
395 0x0, // value
396 FragmentRef::Null(), // FragRef
397 ResolveInfo::Default);
398 f_pEText =
399 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
400 "etext",
401 ResolveInfo::NoType,
402 ResolveInfo::Define,
403 ResolveInfo::Absolute,
404 0x0, // size
405 0x0, // value
406 FragmentRef::Null(), // FragRef
407 ResolveInfo::Default);
408 f_p_EText =
409 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
410 "_etext",
411 ResolveInfo::NoType,
412 ResolveInfo::Define,
413 ResolveInfo::Absolute,
414 0x0, // size
415 0x0, // value
416 FragmentRef::Null(), // FragRef
417 ResolveInfo::Default);
418 f_p__EText =
419 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
420 "__etext",
421 ResolveInfo::NoType,
422 ResolveInfo::Define,
423 ResolveInfo::Absolute,
424 0x0, // size
425 0x0, // value
426 FragmentRef::Null(), // FragRef
427 ResolveInfo::Default);
428 f_pEData =
429 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
430 "edata",
431 ResolveInfo::NoType,
432 ResolveInfo::Define,
433 ResolveInfo::Absolute,
434 0x0, // size
435 0x0, // value
436 FragmentRef::Null(), // FragRef
437 ResolveInfo::Default);
438
439 f_pEnd =
440 pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
441 "end",
442 ResolveInfo::NoType,
443 ResolveInfo::Define,
444 ResolveInfo::Absolute,
445 0x0, // size
446 0x0, // value
447 FragmentRef::Null(), // FragRef
448 ResolveInfo::Default);
449
450 // _edata is defined forcefully.
451 // @ref Google gold linker: defstd.cc: 186
452 f_p_EData =
453 pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
454 "_edata",
455 ResolveInfo::NoType,
456 ResolveInfo::Define,
457 ResolveInfo::Absolute,
458 0x0, // size
459 0x0, // value
460 FragmentRef::Null(), // FragRef
461 ResolveInfo::Default);
462
463 // __bss_start is defined forcefully.
464 // @ref Google gold linker: defstd.cc: 214
465 f_pBSSStart =
466 pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
467 "__bss_start",
468 ResolveInfo::NoType,
469 ResolveInfo::Define,
470 ResolveInfo::Absolute,
471 0x0, // size
472 0x0, // value
473 FragmentRef::Null(), // FragRef
474 ResolveInfo::Default);
475
476 // _end is defined forcefully.
477 // @ref Google gold linker: defstd.cc: 228
478 f_p_End =
479 pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
480 "_end",
481 ResolveInfo::NoType,
482 ResolveInfo::Define,
483 ResolveInfo::Absolute,
484 0x0, // size
485 0x0, // value
486 FragmentRef::Null(), // FragRef
487 ResolveInfo::Default);
488
489 return true;
490 }
491
finalizeStandardSymbols()492 bool GNULDBackend::finalizeStandardSymbols()
493 {
494 if (LinkerConfig::Object == config().codeGenType())
495 return true;
496
497 ELFFileFormat* file_format = getOutputFormat();
498
499 // ----- section symbols ----- //
500 if (NULL != f_pPreInitArrayStart) {
501 if (!f_pPreInitArrayStart->hasFragRef()) {
502 f_pPreInitArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute);
503 f_pPreInitArrayStart->setValue(0x0);
504 }
505 }
506
507 if (NULL != f_pPreInitArrayEnd) {
508 if (f_pPreInitArrayEnd->hasFragRef()) {
509 f_pPreInitArrayEnd->setValue(f_pPreInitArrayEnd->value() +
510 file_format->getPreInitArray().size());
511 }
512 else {
513 f_pPreInitArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute);
514 f_pPreInitArrayEnd->setValue(0x0);
515 }
516 }
517
518 if (NULL != f_pInitArrayStart) {
519 if (!f_pInitArrayStart->hasFragRef()) {
520 f_pInitArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute);
521 f_pInitArrayStart->setValue(0x0);
522 }
523 }
524
525 if (NULL != f_pInitArrayEnd) {
526 if (f_pInitArrayEnd->hasFragRef()) {
527 f_pInitArrayEnd->setValue(f_pInitArrayEnd->value() +
528 file_format->getInitArray().size());
529 }
530 else {
531 f_pInitArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute);
532 f_pInitArrayEnd->setValue(0x0);
533 }
534 }
535
536 if (NULL != f_pFiniArrayStart) {
537 if (!f_pFiniArrayStart->hasFragRef()) {
538 f_pFiniArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute);
539 f_pFiniArrayStart->setValue(0x0);
540 }
541 }
542
543 if (NULL != f_pFiniArrayEnd) {
544 if (f_pFiniArrayEnd->hasFragRef()) {
545 f_pFiniArrayEnd->setValue(f_pFiniArrayEnd->value() +
546 file_format->getFiniArray().size());
547 }
548 else {
549 f_pFiniArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute);
550 f_pFiniArrayEnd->setValue(0x0);
551 }
552 }
553
554 if (NULL != f_pStack) {
555 if (!f_pStack->hasFragRef()) {
556 f_pStack->resolveInfo()->setBinding(ResolveInfo::Absolute);
557 f_pStack->setValue(0x0);
558 }
559 }
560
561 if (NULL != f_pDynamic) {
562 f_pDynamic->resolveInfo()->setBinding(ResolveInfo::Local);
563 f_pDynamic->setValue(file_format->getDynamic().addr());
564 f_pDynamic->setSize(file_format->getDynamic().size());
565 }
566
567 // ----- segment symbols ----- //
568 if (NULL != f_pExecutableStart) {
569 ELFSegment* exec_start = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD, 0x0, 0x0);
570 if (NULL != exec_start) {
571 if (ResolveInfo::ThreadLocal != f_pExecutableStart->type()) {
572 f_pExecutableStart->setValue(f_pExecutableStart->value() +
573 exec_start->vaddr());
574 }
575 }
576 else
577 f_pExecutableStart->setValue(0x0);
578 }
579
580 if (NULL != f_pEText || NULL != f_p_EText || NULL !=f_p__EText) {
581 ELFSegment* etext = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD,
582 llvm::ELF::PF_X,
583 llvm::ELF::PF_W);
584 if (NULL != etext) {
585 if (NULL != f_pEText && ResolveInfo::ThreadLocal != f_pEText->type()) {
586 f_pEText->setValue(f_pEText->value() +
587 etext->vaddr() +
588 etext->memsz());
589 }
590 if (NULL != f_p_EText && ResolveInfo::ThreadLocal != f_p_EText->type()) {
591 f_p_EText->setValue(f_p_EText->value() +
592 etext->vaddr() +
593 etext->memsz());
594 }
595 if (NULL != f_p__EText && ResolveInfo::ThreadLocal != f_p__EText->type()) {
596 f_p__EText->setValue(f_p__EText->value() +
597 etext->vaddr() +
598 etext->memsz());
599 }
600 }
601 else {
602 if (NULL != f_pEText)
603 f_pEText->setValue(0x0);
604 if (NULL != f_p_EText)
605 f_p_EText->setValue(0x0);
606 if (NULL != f_p__EText)
607 f_p__EText->setValue(0x0);
608 }
609 }
610
611 if (NULL != f_pEData || NULL != f_p_EData || NULL != f_pBSSStart ||
612 NULL != f_pEnd || NULL != f_p_End) {
613 ELFSegment* edata = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD,
614 llvm::ELF::PF_W,
615 0x0);
616 if (NULL != edata) {
617 if (NULL != f_pEData && ResolveInfo::ThreadLocal != f_pEData->type()) {
618 f_pEData->setValue(f_pEData->value() +
619 edata->vaddr() +
620 edata->filesz());
621 }
622 if (NULL != f_p_EData && ResolveInfo::ThreadLocal != f_p_EData->type()) {
623 f_p_EData->setValue(f_p_EData->value() +
624 edata->vaddr() +
625 edata->filesz());
626 }
627 if (NULL != f_pBSSStart && ResolveInfo::ThreadLocal != f_pBSSStart->type()) {
628 f_pBSSStart->setValue(f_pBSSStart->value() +
629 edata->vaddr() +
630 edata->filesz());
631 }
632
633 if (NULL != f_pEnd && ResolveInfo::ThreadLocal != f_pEnd->type()) {
634 f_pEnd->setValue(f_pEnd->value() +
635 edata->vaddr() +
636 edata->memsz());
637 }
638 if (NULL != f_p_End && ResolveInfo::ThreadLocal != f_p_End->type()) {
639 f_p_End->setValue(f_p_End->value() +
640 edata->vaddr() +
641 edata->memsz());
642 }
643 }
644 else {
645 if (NULL != f_pEData)
646 f_pEData->setValue(0x0);
647 if (NULL != f_p_EData)
648 f_p_EData->setValue(0x0);
649 if (NULL != f_pBSSStart)
650 f_pBSSStart->setValue(0x0);
651
652 if (NULL != f_pEnd)
653 f_pEnd->setValue(0x0);
654 if (NULL != f_p_End)
655 f_p_End->setValue(0x0);
656 }
657 }
658
659 return true;
660 }
661
finalizeTLSSymbol(LDSymbol & pSymbol)662 bool GNULDBackend::finalizeTLSSymbol(LDSymbol& pSymbol)
663 {
664 // ignore if symbol has no fragRef
665 if (!pSymbol.hasFragRef())
666 return true;
667
668 // the value of a TLS symbol is the offset to the TLS segment
669 ELFSegment* tls_seg = m_ELFSegmentTable.find(llvm::ELF::PT_TLS,
670 llvm::ELF::PF_R, 0x0);
671 uint64_t value = pSymbol.fragRef()->getOutputOffset();
672 uint64_t addr = pSymbol.fragRef()->frag()->getParent()->getSection().addr();
673 pSymbol.setValue(value + addr - tls_seg->vaddr());
674 return true;
675 }
676
getOutputFormat()677 ELFFileFormat* GNULDBackend::getOutputFormat()
678 {
679 switch (config().codeGenType()) {
680 case LinkerConfig::DynObj:
681 assert(NULL != m_pDynObjFileFormat);
682 return m_pDynObjFileFormat;
683 case LinkerConfig::Exec:
684 case LinkerConfig::Binary:
685 assert(NULL != m_pExecFileFormat);
686 return m_pExecFileFormat;
687 case LinkerConfig::Object:
688 assert(NULL != m_pObjectFileFormat);
689 return m_pObjectFileFormat;
690 default:
691 fatal(diag::unrecognized_output_file) << config().codeGenType();
692 return NULL;
693 }
694 }
695
getOutputFormat() const696 const ELFFileFormat* GNULDBackend::getOutputFormat() const
697 {
698 switch (config().codeGenType()) {
699 case LinkerConfig::DynObj:
700 assert(NULL != m_pDynObjFileFormat);
701 return m_pDynObjFileFormat;
702 case LinkerConfig::Exec:
703 case LinkerConfig::Binary:
704 assert(NULL != m_pExecFileFormat);
705 return m_pExecFileFormat;
706 case LinkerConfig::Object:
707 assert(NULL != m_pObjectFileFormat);
708 return m_pObjectFileFormat;
709 default:
710 fatal(diag::unrecognized_output_file) << config().codeGenType();
711 return NULL;
712 }
713 }
714
partialScanRelocation(Relocation & pReloc,Module & pModule,const LDSection & pSection)715 void GNULDBackend::partialScanRelocation(Relocation& pReloc,
716 Module& pModule,
717 const LDSection& pSection)
718 {
719 // if we meet a section symbol
720 if (pReloc.symInfo()->type() == ResolveInfo::Section) {
721 LDSymbol* input_sym = pReloc.symInfo()->outSymbol();
722
723 // 1. update the relocation target offset
724 assert(input_sym->hasFragRef());
725 uint64_t offset = input_sym->fragRef()->getOutputOffset();
726 pReloc.target() += offset;
727
728 // 2. get output section symbol
729 // get the output LDSection which the symbol defined in
730 const LDSection& out_sect =
731 input_sym->fragRef()->frag()->getParent()->getSection();
732 ResolveInfo* sym_info = pModule.getSectionSymbolSet().get(out_sect)->resolveInfo();
733 // set relocation target symbol to the output section symbol's resolveInfo
734 pReloc.setSymInfo(sym_info);
735 }
736 }
737
738 /// sizeNamePools - compute the size of regular name pools
739 /// In ELF executable files, regular name pools are .symtab, .strtab,
740 /// .dynsym, .dynstr, .hash and .shstrtab.
sizeNamePools(Module & pModule,bool pIsStaticLink)741 void GNULDBackend::sizeNamePools(Module& pModule, bool pIsStaticLink)
742 {
743 // number of entries in symbol tables starts from 1 to hold the special entry
744 // at index 0 (STN_UNDEF). See ELF Spec Book I, p1-21.
745 size_t symtab = 1;
746 size_t dynsym = pIsStaticLink ? 0 : 1;
747
748 // size of string tables starts from 1 to hold the null character in their
749 // first byte
750 size_t strtab = 1;
751 size_t dynstr = pIsStaticLink ? 0 : 1;
752 size_t shstrtab = 1;
753 size_t hash = 0;
754 size_t gnuhash = 0;
755
756 // number of local symbol in the .symtab and .dynsym
757 size_t symtab_local_cnt = 0;
758 size_t dynsym_local_cnt = 0;
759
760 Module::SymbolTable& symbols = pModule.getSymbolTable();
761 Module::const_sym_iterator symbol, symEnd;
762 /// Compute the size of .symtab, .strtab, and symtab_local_cnt
763 /// @{
764 symEnd = symbols.end();
765 for (symbol = symbols.begin(); symbol != symEnd; ++symbol) {
766 ++symtab;
767 if (ResolveInfo::Section != (*symbol)->type())
768 strtab += (*symbol)->nameSize() + 1;
769 }
770 symtab_local_cnt = 1 + symbols.numOfFiles() + symbols.numOfLocals() +
771 symbols.numOfLocalDyns();
772
773 ELFFileFormat* file_format = getOutputFormat();
774
775 switch(config().codeGenType()) {
776 case LinkerConfig::DynObj: {
777 // soname
778 dynstr += pModule.name().size() + 1;
779 }
780 /** fall through **/
781 case LinkerConfig::Exec:
782 case LinkerConfig::Binary: {
783 if (!pIsStaticLink) {
784 /// Compute the size of .dynsym, .dynstr, and dynsym_local_cnt
785 symEnd = symbols.dynamicEnd();
786 for (symbol = symbols.localDynBegin(); symbol != symEnd; ++symbol) {
787 ++dynsym;
788 if (ResolveInfo::Section != (*symbol)->type())
789 dynstr += (*symbol)->nameSize() + 1;
790 }
791 dynsym_local_cnt = 1 + symbols.numOfLocalDyns();
792
793 // compute .gnu.hash
794 if (GeneralOptions::GNU == config().options().getHashStyle() ||
795 GeneralOptions::Both == config().options().getHashStyle()) {
796 // count the number of dynsym to hash
797 size_t hashed_sym_cnt = 0;
798 symEnd = symbols.dynamicEnd();
799 for (symbol = symbols.dynamicBegin(); symbol != symEnd; ++symbol) {
800 if (DynsymCompare().needGNUHash(**symbol))
801 ++hashed_sym_cnt;
802 }
803 // Special case for empty .dynsym
804 if (hashed_sym_cnt == 0)
805 gnuhash = 5 * 4 + config().targets().bitclass() / 8;
806 else {
807 size_t nbucket = getHashBucketCount(hashed_sym_cnt, true);
808 gnuhash = (4 + nbucket + hashed_sym_cnt) * 4;
809 gnuhash += (1U << getGNUHashMaskbitslog2(hashed_sym_cnt)) / 8;
810 }
811 }
812
813 // compute .hash
814 if (GeneralOptions::SystemV == config().options().getHashStyle() ||
815 GeneralOptions::Both == config().options().getHashStyle()) {
816 // Both Elf32_Word and Elf64_Word are 4 bytes
817 hash = (2 + getHashBucketCount(dynsym, false) + dynsym) *
818 sizeof(llvm::ELF::Elf32_Word);
819 }
820
821 // add DT_NEEDED
822 Module::const_lib_iterator lib, libEnd = pModule.lib_end();
823 for (lib = pModule.lib_begin(); lib != libEnd; ++lib) {
824 if (!(*lib)->attribute()->isAsNeeded() || (*lib)->isNeeded()) {
825 dynstr += (*lib)->name().size() + 1;
826 dynamic().reserveNeedEntry();
827 }
828 }
829
830 // add DT_RPATH
831 if (!config().options().getRpathList().empty()) {
832 dynamic().reserveNeedEntry();
833 GeneralOptions::const_rpath_iterator rpath,
834 rpathEnd = config().options().rpath_end();
835 for (rpath = config().options().rpath_begin();
836 rpath != rpathEnd; ++rpath)
837 dynstr += (*rpath).size() + 1;
838 }
839
840 // set size
841 if (config().targets().is32Bits()) {
842 file_format->getDynSymTab().setSize(dynsym *
843 sizeof(llvm::ELF::Elf32_Sym));
844 } else {
845 file_format->getDynSymTab().setSize(dynsym *
846 sizeof(llvm::ELF::Elf64_Sym));
847 }
848 file_format->getDynStrTab().setSize(dynstr);
849 file_format->getHashTab().setSize(hash);
850 file_format->getGNUHashTab().setSize(gnuhash);
851
852 // set .dynsym sh_info to one greater than the symbol table
853 // index of the last local symbol
854 file_format->getDynSymTab().setInfo(dynsym_local_cnt);
855
856 // Because some entries in .dynamic section need information of .dynsym,
857 // .dynstr, .symtab, .strtab and .hash, we can not reserve non-DT_NEEDED
858 // entries until we get the size of the sections mentioned above
859 dynamic().reserveEntries(*file_format);
860 file_format->getDynamic().setSize(dynamic().numOfBytes());
861 }
862 }
863 /* fall through */
864 case LinkerConfig::Object: {
865 if (config().targets().is32Bits())
866 file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf32_Sym));
867 else
868 file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf64_Sym));
869 file_format->getStrTab().setSize(strtab);
870
871 // set .symtab sh_info to one greater than the symbol table
872 // index of the last local symbol
873 file_format->getSymTab().setInfo(symtab_local_cnt);
874
875 // compute the size of .shstrtab section.
876 Module::const_iterator sect, sectEnd = pModule.end();
877 for (sect = pModule.begin(); sect != sectEnd; ++sect) {
878 switch ((*sect)->kind()) {
879 case LDFileFormat::Null:
880 break;
881 // take StackNote directly
882 case LDFileFormat::StackNote:
883 shstrtab += ((*sect)->name().size() + 1);
884 break;
885 case LDFileFormat::EhFrame:
886 if (((*sect)->size() != 0) ||
887 ((*sect)->hasEhFrame() &&
888 config().codeGenType() == LinkerConfig::Object))
889 shstrtab += ((*sect)->name().size() + 1);
890 break;
891 case LDFileFormat::Relocation:
892 if (((*sect)->size() != 0) ||
893 ((*sect)->hasRelocData() &&
894 config().codeGenType() == LinkerConfig::Object))
895 shstrtab += ((*sect)->name().size() + 1);
896 break;
897 default:
898 if (((*sect)->size() != 0) ||
899 ((*sect)->hasSectionData() &&
900 config().codeGenType() == LinkerConfig::Object))
901 shstrtab += ((*sect)->name().size() + 1);
902 break;
903 } // end of switch
904 } // end of for
905 shstrtab += (strlen(".shstrtab") + 1);
906 file_format->getShStrTab().setSize(shstrtab);
907 break;
908 }
909 default:
910 fatal(diag::fatal_illegal_codegen_type) << pModule.name();
911 break;
912 } // end of switch
913 }
914
915 /// emitSymbol32 - emit an ELF32 symbol
emitSymbol32(llvm::ELF::Elf32_Sym & pSym,LDSymbol & pSymbol,char * pStrtab,size_t pStrtabsize,size_t pSymtabIdx)916 void GNULDBackend::emitSymbol32(llvm::ELF::Elf32_Sym& pSym,
917 LDSymbol& pSymbol,
918 char* pStrtab,
919 size_t pStrtabsize,
920 size_t pSymtabIdx)
921 {
922 // FIXME: check the endian between host and target
923 // write out symbol
924 if (ResolveInfo::Section != pSymbol.type()) {
925 pSym.st_name = pStrtabsize;
926 strcpy((pStrtab + pStrtabsize), pSymbol.name());
927 }
928 else {
929 pSym.st_name = 0;
930 }
931 pSym.st_value = pSymbol.value();
932 pSym.st_size = getSymbolSize(pSymbol);
933 pSym.st_info = getSymbolInfo(pSymbol);
934 pSym.st_other = pSymbol.visibility();
935 pSym.st_shndx = getSymbolShndx(pSymbol);
936 }
937
938 /// emitSymbol64 - emit an ELF64 symbol
emitSymbol64(llvm::ELF::Elf64_Sym & pSym,LDSymbol & pSymbol,char * pStrtab,size_t pStrtabsize,size_t pSymtabIdx)939 void GNULDBackend::emitSymbol64(llvm::ELF::Elf64_Sym& pSym,
940 LDSymbol& pSymbol,
941 char* pStrtab,
942 size_t pStrtabsize,
943 size_t pSymtabIdx)
944 {
945 // FIXME: check the endian between host and target
946 // write out symbol
947 if (ResolveInfo::Section != pSymbol.type()) {
948 pSym.st_name = pStrtabsize;
949 strcpy((pStrtab + pStrtabsize), pSymbol.name());
950 }
951 else {
952 pSym.st_name = 0;
953 }
954 pSym.st_name = pStrtabsize;
955 pSym.st_value = pSymbol.value();
956 pSym.st_size = getSymbolSize(pSymbol);
957 pSym.st_info = getSymbolInfo(pSymbol);
958 pSym.st_other = pSymbol.visibility();
959 pSym.st_shndx = getSymbolShndx(pSymbol);
960 }
961
962 /// emitRegNamePools - emit regular name pools - .symtab, .strtab
963 ///
964 /// the size of these tables should be computed before layout
965 /// layout should computes the start offset of these tables
emitRegNamePools(const Module & pModule,MemoryArea & pOutput)966 void GNULDBackend::emitRegNamePools(const Module& pModule,
967 MemoryArea& pOutput)
968 {
969 ELFFileFormat* file_format = getOutputFormat();
970
971 LDSection& symtab_sect = file_format->getSymTab();
972 LDSection& strtab_sect = file_format->getStrTab();
973
974 MemoryRegion* symtab_region = pOutput.request(symtab_sect.offset(),
975 symtab_sect.size());
976 MemoryRegion* strtab_region = pOutput.request(strtab_sect.offset(),
977 strtab_sect.size());
978
979 // set up symtab_region
980 llvm::ELF::Elf32_Sym* symtab32 = NULL;
981 llvm::ELF::Elf64_Sym* symtab64 = NULL;
982 if (config().targets().is32Bits())
983 symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start();
984 else if (config().targets().is64Bits())
985 symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start();
986 else {
987 fatal(diag::unsupported_bitclass) << config().targets().triple().str()
988 << config().targets().bitclass();
989 }
990
991 // set up strtab_region
992 char* strtab = (char*)strtab_region->start();
993
994 // emit the first ELF symbol
995 if (config().targets().is32Bits())
996 emitSymbol32(symtab32[0], *LDSymbol::Null(), strtab, 0, 0);
997 else
998 emitSymbol64(symtab64[0], *LDSymbol::Null(), strtab, 0, 0);
999
1000 bool sym_exist = false;
1001 HashTableType::entry_type* entry = NULL;
1002 if (LinkerConfig::Object == config().codeGenType()) {
1003 entry = m_pSymIndexMap->insert(LDSymbol::Null(), sym_exist);
1004 entry->setValue(0);
1005 }
1006
1007 size_t symIdx = 1;
1008 size_t strtabsize = 1;
1009
1010 const Module::SymbolTable& symbols = pModule.getSymbolTable();
1011 Module::const_sym_iterator symbol, symEnd;
1012
1013 symEnd = symbols.end();
1014 for (symbol = symbols.begin(); symbol != symEnd; ++symbol) {
1015 if (LinkerConfig::Object == config().codeGenType()) {
1016 entry = m_pSymIndexMap->insert(*symbol, sym_exist);
1017 entry->setValue(symIdx);
1018 }
1019 if (config().targets().is32Bits())
1020 emitSymbol32(symtab32[symIdx], **symbol, strtab, strtabsize, symIdx);
1021 else
1022 emitSymbol64(symtab64[symIdx], **symbol, strtab, strtabsize, symIdx);
1023 ++symIdx;
1024 if (ResolveInfo::Section != (*symbol)->type())
1025 strtabsize += (*symbol)->nameSize() + 1;
1026 }
1027 }
1028
1029 /// emitDynNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash
1030 ///
1031 /// the size of these tables should be computed before layout
1032 /// layout should computes the start offset of these tables
emitDynNamePools(Module & pModule,MemoryArea & pOutput)1033 void GNULDBackend::emitDynNamePools(Module& pModule, MemoryArea& pOutput)
1034 {
1035 ELFFileFormat* file_format = getOutputFormat();
1036 if (!file_format->hasDynSymTab() ||
1037 !file_format->hasDynStrTab() ||
1038 !file_format->hasDynamic())
1039 return;
1040
1041 bool sym_exist = false;
1042 HashTableType::entry_type* entry = 0;
1043
1044 LDSection& symtab_sect = file_format->getDynSymTab();
1045 LDSection& strtab_sect = file_format->getDynStrTab();
1046 LDSection& dyn_sect = file_format->getDynamic();
1047
1048 MemoryRegion* symtab_region = pOutput.request(symtab_sect.offset(),
1049 symtab_sect.size());
1050 MemoryRegion* strtab_region = pOutput.request(strtab_sect.offset(),
1051 strtab_sect.size());
1052 MemoryRegion* dyn_region = pOutput.request(dyn_sect.offset(),
1053 dyn_sect.size());
1054 // set up symtab_region
1055 llvm::ELF::Elf32_Sym* symtab32 = NULL;
1056 llvm::ELF::Elf64_Sym* symtab64 = NULL;
1057 if (config().targets().is32Bits())
1058 symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start();
1059 else if (config().targets().is64Bits())
1060 symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start();
1061 else {
1062 fatal(diag::unsupported_bitclass) << config().targets().triple().str()
1063 << config().targets().bitclass();
1064 }
1065
1066 // set up strtab_region
1067 char* strtab = (char*)strtab_region->start();
1068
1069 // emit the first ELF symbol
1070 if (config().targets().is32Bits())
1071 emitSymbol32(symtab32[0], *LDSymbol::Null(), strtab, 0, 0);
1072 else
1073 emitSymbol64(symtab64[0], *LDSymbol::Null(), strtab, 0, 0);
1074
1075 size_t symIdx = 1;
1076 size_t strtabsize = 1;
1077
1078 Module::SymbolTable& symbols = pModule.getSymbolTable();
1079 // emit .gnu.hash
1080 if (GeneralOptions::GNU == config().options().getHashStyle() ||
1081 GeneralOptions::Both == config().options().getHashStyle()) {
1082 // Currently we may add output symbols after sizeNamePools(), and a
1083 // non-stable sort is used in SymbolCategory::arrange(), so we just
1084 // sort .dynsym right before emitting .gnu.hash
1085 std::stable_sort(symbols.dynamicBegin(), symbols.dynamicEnd(),
1086 DynsymCompare());
1087 emitGNUHashTab(symbols, pOutput);
1088 }
1089 // emit .hash
1090 if (GeneralOptions::SystemV == config().options().getHashStyle() ||
1091 GeneralOptions::Both == config().options().getHashStyle())
1092 emitELFHashTab(symbols, pOutput);
1093
1094 // emit .dynsym, and .dynstr (emit LocalDyn and Dynamic category)
1095 Module::const_sym_iterator symbol, symEnd = symbols.dynamicEnd();
1096 for (symbol = symbols.localDynBegin(); symbol != symEnd; ++symbol) {
1097 if (config().targets().is32Bits())
1098 emitSymbol32(symtab32[symIdx], **symbol, strtab, strtabsize, symIdx);
1099 else
1100 emitSymbol64(symtab64[symIdx], **symbol, strtab, strtabsize, symIdx);
1101 // maintain output's symbol and index map
1102 entry = m_pSymIndexMap->insert(*symbol, sym_exist);
1103 entry->setValue(symIdx);
1104 // sum up counters
1105 ++symIdx;
1106 if (ResolveInfo::Section != (*symbol)->type())
1107 strtabsize += (*symbol)->nameSize() + 1;
1108 }
1109
1110 // emit DT_NEED
1111 // add DT_NEED strings into .dynstr
1112 ELFDynamic::iterator dt_need = dynamic().needBegin();
1113 Module::const_lib_iterator lib, libEnd = pModule.lib_end();
1114 for (lib = pModule.lib_begin(); lib != libEnd; ++lib) {
1115 if (!(*lib)->attribute()->isAsNeeded() || (*lib)->isNeeded()) {
1116 strcpy((strtab + strtabsize), (*lib)->name().c_str());
1117 (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize);
1118 strtabsize += (*lib)->name().size() + 1;
1119 ++dt_need;
1120 }
1121 }
1122
1123 if (!config().options().getRpathList().empty()) {
1124 if (!config().options().hasNewDTags())
1125 (*dt_need)->setValue(llvm::ELF::DT_RPATH, strtabsize);
1126 else
1127 (*dt_need)->setValue(llvm::ELF::DT_RUNPATH, strtabsize);
1128 ++dt_need;
1129
1130 GeneralOptions::const_rpath_iterator rpath,
1131 rpathEnd = config().options().rpath_end();
1132 for (rpath = config().options().rpath_begin(); rpath != rpathEnd; ++rpath) {
1133 memcpy((strtab + strtabsize), (*rpath).data(), (*rpath).size());
1134 strtabsize += (*rpath).size();
1135 strtab[strtabsize++] = (rpath + 1 == rpathEnd ? '\0' : ':');
1136 }
1137 }
1138
1139 // initialize value of ELF .dynamic section
1140 if (LinkerConfig::DynObj == config().codeGenType()) {
1141 // set pointer to SONAME entry in dynamic string table.
1142 dynamic().applySoname(strtabsize);
1143 }
1144 dynamic().applyEntries(*file_format);
1145 dynamic().emit(dyn_sect, *dyn_region);
1146
1147 // emit soname
1148 if (LinkerConfig::DynObj == config().codeGenType()) {
1149 strcpy((strtab + strtabsize), pModule.name().c_str());
1150 strtabsize += pModule.name().size() + 1;
1151 }
1152 }
1153
1154 /// emitELFHashTab - emit .hash
emitELFHashTab(const Module::SymbolTable & pSymtab,MemoryArea & pOutput)1155 void GNULDBackend::emitELFHashTab(const Module::SymbolTable& pSymtab,
1156 MemoryArea& pOutput)
1157 {
1158 ELFFileFormat* file_format = getOutputFormat();
1159 if (!file_format->hasHashTab())
1160 return;
1161 LDSection& hash_sect = file_format->getHashTab();
1162 MemoryRegion* hash_region = pOutput.request(hash_sect.offset(),
1163 hash_sect.size());
1164 // both 32 and 64 bits hash table use 32-bit entry
1165 // set up hash_region
1166 uint32_t* word_array = (uint32_t*)hash_region->start();
1167 uint32_t& nbucket = word_array[0];
1168 uint32_t& nchain = word_array[1];
1169
1170 size_t dynsymSize = 1 + pSymtab.numOfLocalDyns() + pSymtab.numOfDynamics();
1171 nbucket = getHashBucketCount(dynsymSize, false);
1172 nchain = dynsymSize;
1173
1174 uint32_t* bucket = (word_array + 2);
1175 uint32_t* chain = (bucket + nbucket);
1176
1177 // initialize bucket
1178 bzero((void*)bucket, nbucket);
1179
1180 StringHash<ELF> hash_func;
1181
1182 size_t idx = 1;
1183 Module::const_sym_iterator symbol, symEnd = pSymtab.dynamicEnd();
1184 for (symbol = pSymtab.localDynBegin(); symbol != symEnd; ++symbol) {
1185 llvm::StringRef name((*symbol)->name());
1186 size_t bucket_pos = hash_func(name) % nbucket;
1187 chain[idx] = bucket[bucket_pos];
1188 bucket[bucket_pos] = idx;
1189 ++idx;
1190 }
1191 }
1192
1193 /// emitGNUHashTab - emit .gnu.hash
emitGNUHashTab(Module::SymbolTable & pSymtab,MemoryArea & pOutput)1194 void GNULDBackend::emitGNUHashTab(Module::SymbolTable& pSymtab,
1195 MemoryArea& pOutput)
1196 {
1197 ELFFileFormat* file_format = getOutputFormat();
1198 if (!file_format->hasGNUHashTab())
1199 return;
1200
1201 MemoryRegion* gnuhash_region =
1202 pOutput.request(file_format->getGNUHashTab().offset(),
1203 file_format->getGNUHashTab().size());
1204
1205 uint32_t* word_array = (uint32_t*)gnuhash_region->start();
1206 // fixed-length fields
1207 uint32_t& nbucket = word_array[0];
1208 uint32_t& symidx = word_array[1];
1209 uint32_t& maskwords = word_array[2];
1210 uint32_t& shift2 = word_array[3];
1211 // variable-length fields
1212 uint8_t* bitmask = (uint8_t*)(word_array + 4);
1213 uint32_t* bucket = NULL;
1214 uint32_t* chain = NULL;
1215
1216 // count the number of dynsym to hash
1217 size_t unhashed_sym_cnt = pSymtab.numOfLocalDyns();
1218 size_t hashed_sym_cnt = pSymtab.numOfDynamics();
1219 Module::const_sym_iterator symbol, symEnd = pSymtab.dynamicEnd();
1220 for (symbol = pSymtab.dynamicBegin(); symbol != symEnd; ++symbol) {
1221 if (DynsymCompare().needGNUHash(**symbol))
1222 break;
1223 ++unhashed_sym_cnt;
1224 --hashed_sym_cnt;
1225 }
1226
1227 // special case for the empty hash table
1228 if (hashed_sym_cnt == 0) {
1229 nbucket = 1; // one empty bucket
1230 symidx = 1 + unhashed_sym_cnt; // symidx above unhashed symbols
1231 maskwords = 1; // bitmask length
1232 shift2 = 0; // bloom filter
1233
1234 if (config().targets().is32Bits()) {
1235 uint32_t* maskval = (uint32_t*)bitmask;
1236 *maskval = 0; // no valid hashes
1237 } else {
1238 // must be 64
1239 uint64_t* maskval = (uint64_t*)bitmask;
1240 *maskval = 0; // no valid hashes
1241 }
1242 bucket = (uint32_t*)(bitmask + config().targets().bitclass() / 8);
1243 *bucket = 0; // no hash in the only bucket
1244 return;
1245 }
1246
1247 uint32_t maskbitslog2 = getGNUHashMaskbitslog2(hashed_sym_cnt);
1248 uint32_t maskbits = 1u << maskbitslog2;
1249 uint32_t shift1 = config().targets().is32Bits() ? 5 : 6;
1250 uint32_t mask = (1u << shift1) - 1;
1251
1252 nbucket = getHashBucketCount(hashed_sym_cnt, true);
1253 symidx = 1 + unhashed_sym_cnt;
1254 maskwords = 1 << (maskbitslog2 - shift1);
1255 shift2 = maskbitslog2;
1256
1257 // setup bucket and chain
1258 bucket = (uint32_t*)(bitmask + maskbits / 8);
1259 chain = (bucket + nbucket);
1260
1261 // build the gnu style hash table
1262 typedef std::multimap<uint32_t,
1263 std::pair<LDSymbol*, uint32_t> > SymMapType;
1264 SymMapType symmap;
1265 symEnd = pSymtab.dynamicEnd();
1266 for (symbol = pSymtab.localDynBegin() + symidx - 1; symbol != symEnd;
1267 ++symbol) {
1268 StringHash<DJB> hasher;
1269 uint32_t djbhash = hasher((*symbol)->name());
1270 uint32_t hash = djbhash % nbucket;
1271 symmap.insert(std::make_pair(hash, std::make_pair(*symbol, djbhash)));
1272 }
1273
1274 // compute bucket, chain, and bitmask
1275 std::vector<uint64_t> bitmasks(maskwords);
1276 size_t hashedidx = symidx;
1277 for (size_t idx = 0; idx < nbucket; ++idx) {
1278 size_t count = 0;
1279 std::pair<SymMapType::iterator, SymMapType::iterator> ret;
1280 ret = symmap.equal_range(idx);
1281 for (SymMapType::iterator it = ret.first; it != ret.second; ) {
1282 // rearrange the hashed symbol ordering
1283 *(pSymtab.localDynBegin() + hashedidx - 1) = it->second.first;
1284 uint32_t djbhash = it->second.second;
1285 uint32_t val = ((djbhash >> shift1) & ((maskbits >> shift1) - 1));
1286 bitmasks[val] |= 1u << (djbhash & mask);
1287 bitmasks[val] |= 1u << ((djbhash >> shift2) & mask);
1288 val = djbhash & ~1u;
1289 // advance the iterator and check if we're dealing w/ the last elment
1290 if (++it == ret.second) {
1291 // last element terminates the chain
1292 val |= 1;
1293 }
1294 chain[hashedidx - symidx] = val;
1295
1296 ++hashedidx;
1297 ++count;
1298 }
1299
1300 if (count == 0)
1301 bucket[idx] = 0;
1302 else
1303 bucket[idx] = hashedidx - count;
1304 }
1305
1306 // write the bitmasks
1307 if (config().targets().is32Bits()) {
1308 uint32_t* maskval = (uint32_t*)bitmask;
1309 for (size_t i = 0; i < maskwords; ++i)
1310 std::memcpy(maskval + i, &bitmasks[i], 4);
1311 } else {
1312 // must be 64
1313 uint64_t* maskval = (uint64_t*)bitmask;
1314 for (size_t i = 0; i < maskwords; ++i)
1315 std::memcpy(maskval + i, &bitmasks[i], 8);
1316 }
1317 }
1318
1319 /// sizeInterp - compute the size of the .interp section
sizeInterp()1320 void GNULDBackend::sizeInterp()
1321 {
1322 const char* dyld_name;
1323 if (config().options().hasDyld())
1324 dyld_name = config().options().dyld().c_str();
1325 else
1326 dyld_name = m_pInfo->dyld();
1327
1328 LDSection& interp = getOutputFormat()->getInterp();
1329 interp.setSize(std::strlen(dyld_name) + 1);
1330 }
1331
1332 /// emitInterp - emit the .interp
emitInterp(MemoryArea & pOutput)1333 void GNULDBackend::emitInterp(MemoryArea& pOutput)
1334 {
1335 if (getOutputFormat()->hasInterp()) {
1336 const LDSection& interp = getOutputFormat()->getInterp();
1337 MemoryRegion *region = pOutput.request(interp.offset(), interp.size());
1338 const char* dyld_name;
1339 if (config().options().hasDyld())
1340 dyld_name = config().options().dyld().c_str();
1341 else
1342 dyld_name = m_pInfo->dyld();
1343
1344 std::memcpy(region->start(), dyld_name, interp.size());
1345 }
1346 }
1347
1348 /// getSectionOrder
getSectionOrder(const LDSection & pSectHdr) const1349 unsigned int GNULDBackend::getSectionOrder(const LDSection& pSectHdr) const
1350 {
1351 const ELFFileFormat* file_format = getOutputFormat();
1352
1353 // NULL section should be the "1st" section
1354 if (LDFileFormat::Null == pSectHdr.kind())
1355 return 0;
1356
1357 if (&pSectHdr == &file_format->getStrTab())
1358 return SHO_STRTAB;
1359
1360 // if the section is not ALLOC, lay it out until the last possible moment
1361 if (0 == (pSectHdr.flag() & llvm::ELF::SHF_ALLOC))
1362 return SHO_UNDEFINED;
1363
1364 bool is_write = (pSectHdr.flag() & llvm::ELF::SHF_WRITE) != 0;
1365 bool is_exec = (pSectHdr.flag() & llvm::ELF::SHF_EXECINSTR) != 0;
1366 // TODO: need to take care other possible output sections
1367 switch (pSectHdr.kind()) {
1368 case LDFileFormat::Regular:
1369 if (is_exec) {
1370 if (&pSectHdr == &file_format->getInit())
1371 return SHO_INIT;
1372 if (&pSectHdr == &file_format->getFini())
1373 return SHO_FINI;
1374 return SHO_TEXT;
1375 } else if (!is_write) {
1376 return SHO_RO;
1377 } else {
1378 if (config().options().hasRelro()) {
1379 if (&pSectHdr == &file_format->getPreInitArray() ||
1380 &pSectHdr == &file_format->getInitArray() ||
1381 &pSectHdr == &file_format->getFiniArray() ||
1382 &pSectHdr == &file_format->getCtors() ||
1383 &pSectHdr == &file_format->getDtors() ||
1384 &pSectHdr == &file_format->getJCR() ||
1385 &pSectHdr == &file_format->getDataRelRo())
1386 return SHO_RELRO;
1387 if (&pSectHdr == &file_format->getDataRelRoLocal())
1388 return SHO_RELRO_LOCAL;
1389 }
1390 if ((pSectHdr.flag() & llvm::ELF::SHF_TLS) != 0x0) {
1391 return SHO_TLS_DATA;
1392 }
1393 return SHO_DATA;
1394 }
1395
1396 case LDFileFormat::BSS:
1397 if ((pSectHdr.flag() & llvm::ELF::SHF_TLS) != 0x0)
1398 return SHO_TLS_BSS;
1399 return SHO_BSS;
1400
1401 case LDFileFormat::NamePool: {
1402 if (&pSectHdr == &file_format->getDynamic())
1403 return SHO_RELRO;
1404 return SHO_NAMEPOOL;
1405 }
1406 case LDFileFormat::Relocation:
1407 if (&pSectHdr == &file_format->getRelPlt() ||
1408 &pSectHdr == &file_format->getRelaPlt())
1409 return SHO_REL_PLT;
1410 return SHO_RELOCATION;
1411
1412 // get the order from target for target specific sections
1413 case LDFileFormat::Target:
1414 return getTargetSectionOrder(pSectHdr);
1415
1416 // handle .interp and .note.* sections
1417 case LDFileFormat::Note:
1418 if (file_format->hasInterp() && (&pSectHdr == &file_format->getInterp()))
1419 return SHO_INTERP;
1420 else if (is_write)
1421 return SHO_RW_NOTE;
1422 else
1423 return SHO_RO_NOTE;
1424
1425 case LDFileFormat::EhFrame:
1426 case LDFileFormat::EhFrameHdr:
1427 case LDFileFormat::GCCExceptTable:
1428 return SHO_EXCEPTION;
1429
1430 case LDFileFormat::MetaData:
1431 case LDFileFormat::Debug:
1432 default:
1433 return SHO_UNDEFINED;
1434 }
1435 }
1436
1437 /// getSymbolSize
getSymbolSize(const LDSymbol & pSymbol) const1438 uint64_t GNULDBackend::getSymbolSize(const LDSymbol& pSymbol) const
1439 {
1440 // @ref Google gold linker: symtab.cc: 2780
1441 // undefined and dynamic symbols should have zero size.
1442 if (pSymbol.isDyn() || pSymbol.desc() == ResolveInfo::Undefined)
1443 return 0x0;
1444 return pSymbol.resolveInfo()->size();
1445 }
1446
1447 /// getSymbolInfo
getSymbolInfo(const LDSymbol & pSymbol) const1448 uint64_t GNULDBackend::getSymbolInfo(const LDSymbol& pSymbol) const
1449 {
1450 // set binding
1451 uint8_t bind = 0x0;
1452 if (pSymbol.resolveInfo()->isLocal())
1453 bind = llvm::ELF::STB_LOCAL;
1454 else if (pSymbol.resolveInfo()->isGlobal())
1455 bind = llvm::ELF::STB_GLOBAL;
1456 else if (pSymbol.resolveInfo()->isWeak())
1457 bind = llvm::ELF::STB_WEAK;
1458 else if (pSymbol.resolveInfo()->isAbsolute()) {
1459 // (Luba) Is a absolute but not global (weak or local) symbol meaningful?
1460 bind = llvm::ELF::STB_GLOBAL;
1461 }
1462
1463 if (config().codeGenType() != LinkerConfig::Object &&
1464 (pSymbol.visibility() == llvm::ELF::STV_INTERNAL ||
1465 pSymbol.visibility() == llvm::ELF::STV_HIDDEN))
1466 bind = llvm::ELF::STB_LOCAL;
1467
1468 uint32_t type = pSymbol.resolveInfo()->type();
1469 // if the IndirectFunc symbol (i.e., STT_GNU_IFUNC) is from dynobj, change
1470 // its type to Function
1471 if (type == ResolveInfo::IndirectFunc && pSymbol.isDyn())
1472 type = ResolveInfo::Function;
1473 return (type | (bind << 4));
1474 }
1475
1476 /// getSymbolValue - this function is called after layout()
getSymbolValue(const LDSymbol & pSymbol) const1477 uint64_t GNULDBackend::getSymbolValue(const LDSymbol& pSymbol) const
1478 {
1479 if (pSymbol.isDyn())
1480 return 0x0;
1481
1482 return pSymbol.value();
1483 }
1484
1485 /// getSymbolShndx - this function is called after layout()
1486 uint64_t
getSymbolShndx(const LDSymbol & pSymbol) const1487 GNULDBackend::getSymbolShndx(const LDSymbol& pSymbol) const
1488 {
1489 if (pSymbol.resolveInfo()->isAbsolute())
1490 return llvm::ELF::SHN_ABS;
1491 if (pSymbol.resolveInfo()->isCommon())
1492 return llvm::ELF::SHN_COMMON;
1493 if (pSymbol.resolveInfo()->isUndef() || pSymbol.isDyn())
1494 return llvm::ELF::SHN_UNDEF;
1495
1496 if (pSymbol.resolveInfo()->isLocal() &&
1497 LinkerConfig::Object != config().codeGenType()) {
1498 switch (pSymbol.type()) {
1499 case ResolveInfo::NoType:
1500 case ResolveInfo::File:
1501 return llvm::ELF::SHN_ABS;
1502 }
1503 }
1504
1505 if (pSymbol.resolveInfo()->isDefine() && !pSymbol.hasFragRef())
1506 return llvm::ELF::SHN_ABS;
1507
1508 assert(pSymbol.hasFragRef() && "symbols must have fragment reference to get its index");
1509 return pSymbol.fragRef()->frag()->getParent()->getSection().index();
1510 }
1511
1512 /// getSymbolIdx - called by emitRelocation to get the ouput symbol table index
getSymbolIdx(const LDSymbol * pSymbol) const1513 size_t GNULDBackend::getSymbolIdx(const LDSymbol* pSymbol) const
1514 {
1515 HashTableType::iterator entry = m_pSymIndexMap->find(const_cast<LDSymbol *>(pSymbol));
1516 assert(entry != m_pSymIndexMap->end() && "symbol not found in the symbol table");
1517 return entry.getEntry()->value();
1518 }
1519
1520 /// isTemporary - Whether pSymbol is a local label.
isTemporary(const LDSymbol & pSymbol) const1521 bool GNULDBackend::isTemporary(const LDSymbol& pSymbol) const
1522 {
1523 if (ResolveInfo::Local != pSymbol.binding())
1524 return false;
1525
1526 if (pSymbol.nameSize() < 2)
1527 return false;
1528
1529 const char* name = pSymbol.name();
1530 if ('.' == name[0] && 'L' == name[1])
1531 return true;
1532
1533 // UnixWare 2.1 cc generate DWARF debugging symbols with `..' prefix.
1534 // @ref Google gold linker, target.cc:39 @@ Target::do_is_local_label_name()
1535 if (name[0] == '.' && name[1] == '.')
1536 return true;
1537
1538 // Work arround for gcc's bug
1539 // gcc sometimes generate symbols with '_.L_' prefix.
1540 // @ref Google gold linker, target.cc:39 @@ Target::do_is_local_label_name()
1541 if (pSymbol.nameSize() < 4)
1542 return false;
1543
1544 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
1545 return true;
1546
1547 return false;
1548 }
1549
1550 /// allocateCommonSymbols - allocate common symbols in the corresponding
1551 /// sections. This is executed at pre-layout stage.
1552 /// @refer Google gold linker: common.cc: 214
1553 bool
allocateCommonSymbols(Module & pModule)1554 GNULDBackend::allocateCommonSymbols(Module& pModule)
1555 {
1556 SymbolCategory& symbol_list = pModule.getSymbolTable();
1557
1558 if (symbol_list.emptyCommons() && symbol_list.emptyFiles() &&
1559 symbol_list.emptyLocals() && symbol_list.emptyLocalDyns())
1560 return true;
1561
1562 SymbolCategory::iterator com_sym, com_end;
1563
1564 // FIXME: If the order of common symbols is defined, then sort common symbols
1565 // std::sort(com_sym, com_end, some kind of order);
1566
1567 // get corresponding BSS LDSection
1568 ELFFileFormat* file_format = getOutputFormat();
1569 LDSection& bss_sect = file_format->getBSS();
1570 LDSection& tbss_sect = file_format->getTBSS();
1571
1572 // get or create corresponding BSS SectionData
1573 SectionData* bss_sect_data = NULL;
1574 if (bss_sect.hasSectionData())
1575 bss_sect_data = bss_sect.getSectionData();
1576 else
1577 bss_sect_data = IRBuilder::CreateSectionData(bss_sect);
1578
1579 SectionData* tbss_sect_data = NULL;
1580 if (tbss_sect.hasSectionData())
1581 tbss_sect_data = tbss_sect.getSectionData();
1582 else
1583 tbss_sect_data = IRBuilder::CreateSectionData(tbss_sect);
1584
1585 // remember original BSS size
1586 uint64_t bss_offset = bss_sect.size();
1587 uint64_t tbss_offset = tbss_sect.size();
1588
1589 // allocate all local common symbols
1590 com_end = symbol_list.localEnd();
1591
1592 for (com_sym = symbol_list.localBegin(); com_sym != com_end; ++com_sym) {
1593 if (ResolveInfo::Common == (*com_sym)->desc()) {
1594 // We have to reset the description of the symbol here. When doing
1595 // incremental linking, the output relocatable object may have common
1596 // symbols. Therefore, we can not treat common symbols as normal symbols
1597 // when emitting the regular name pools. We must change the symbols'
1598 // description here.
1599 (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define);
1600 Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size());
1601 (*com_sym)->setFragmentRef(FragmentRef::Create(*frag, 0));
1602
1603 if (ResolveInfo::ThreadLocal == (*com_sym)->type()) {
1604 // allocate TLS common symbol in tbss section
1605 tbss_offset += ObjectBuilder::AppendFragment(*frag,
1606 *tbss_sect_data,
1607 (*com_sym)->value());
1608 }
1609 else {
1610 bss_offset += ObjectBuilder::AppendFragment(*frag,
1611 *bss_sect_data,
1612 (*com_sym)->value());
1613 }
1614 }
1615 }
1616
1617 // allocate all global common symbols
1618 com_end = symbol_list.commonEnd();
1619 for (com_sym = symbol_list.commonBegin(); com_sym != com_end; ++com_sym) {
1620 // We have to reset the description of the symbol here. When doing
1621 // incremental linking, the output relocatable object may have common
1622 // symbols. Therefore, we can not treat common symbols as normal symbols
1623 // when emitting the regular name pools. We must change the symbols'
1624 // description here.
1625 (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define);
1626 Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size());
1627 (*com_sym)->setFragmentRef(FragmentRef::Create(*frag, 0));
1628
1629 if (ResolveInfo::ThreadLocal == (*com_sym)->type()) {
1630 // allocate TLS common symbol in tbss section
1631 tbss_offset += ObjectBuilder::AppendFragment(*frag,
1632 *tbss_sect_data,
1633 (*com_sym)->value());
1634 }
1635 else {
1636 bss_offset += ObjectBuilder::AppendFragment(*frag,
1637 *bss_sect_data,
1638 (*com_sym)->value());
1639 }
1640 }
1641
1642 bss_sect.setSize(bss_offset);
1643 tbss_sect.setSize(tbss_offset);
1644 symbol_list.changeCommonsToGlobal();
1645 return true;
1646 }
1647
1648 /// updateSectionFlags - update pTo's flags when merging pFrom
1649 /// update the output section flags based on input section flags.
1650 /// @ref The Google gold linker:
1651 /// output.cc: 2809: Output_section::update_flags_for_input_section
updateSectionFlags(LDSection & pTo,const LDSection & pFrom)1652 bool GNULDBackend::updateSectionFlags(LDSection& pTo, const LDSection& pFrom)
1653 {
1654 // union the flags from input
1655 uint32_t flags = pTo.flag();
1656 flags |= (pFrom.flag() &
1657 (llvm::ELF::SHF_WRITE |
1658 llvm::ELF::SHF_ALLOC |
1659 llvm::ELF::SHF_EXECINSTR));
1660
1661 // if there is an input section is not SHF_MERGE, clean this flag
1662 if (0 == (pFrom.flag() & llvm::ELF::SHF_MERGE))
1663 flags &= ~llvm::ELF::SHF_MERGE;
1664
1665 // if there is an input section is not SHF_STRINGS, clean this flag
1666 if (0 == (pFrom.flag() & llvm::ELF::SHF_STRINGS))
1667 flags &= ~llvm::ELF::SHF_STRINGS;
1668
1669 pTo.setFlag(flags);
1670 return true;
1671 }
1672
1673 /// createProgramHdrs - base on output sections to create the program headers
createProgramHdrs(Module & pModule)1674 void GNULDBackend::createProgramHdrs(Module& pModule)
1675 {
1676 ELFFileFormat *file_format = getOutputFormat();
1677
1678 // make PT_PHDR
1679 m_ELFSegmentTable.produce(llvm::ELF::PT_PHDR);
1680
1681 // make PT_INTERP
1682 if (file_format->hasInterp()) {
1683 ELFSegment* interp_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_INTERP);
1684 interp_seg->addSection(&file_format->getInterp());
1685 }
1686
1687 uint32_t cur_flag, prev_flag = getSegmentFlag(0);
1688 ELFSegment* load_seg = NULL;
1689 // make possible PT_LOAD segments
1690 Module::iterator sect, sect_end = pModule.end();
1691 for (sect = pModule.begin(); sect != sect_end; ++sect) {
1692
1693 if (0 == ((*sect)->flag() & llvm::ELF::SHF_ALLOC) &&
1694 LDFileFormat::Null != (*sect)->kind())
1695 continue;
1696
1697 cur_flag = getSegmentFlag((*sect)->flag());
1698 bool createPT_LOAD = false;
1699 if (LDFileFormat::Null == (*sect)->kind()) {
1700 // 1. create text segment
1701 createPT_LOAD = true;
1702 }
1703 else if (!config().options().omagic() &&
1704 (prev_flag & llvm::ELF::PF_W) ^ (cur_flag & llvm::ELF::PF_W)) {
1705 // 2. create data segment if w/o omagic set
1706 createPT_LOAD = true;
1707 }
1708 else if ((*sect)->kind() == LDFileFormat::BSS &&
1709 load_seg->isDataSegment() &&
1710 config().scripts().addressMap().find(".bss") !=
1711 (config().scripts().addressMap().end())) {
1712 // 3. create bss segment if w/ -Tbss and there is a data segment
1713 createPT_LOAD = true;
1714 }
1715 else {
1716 if ((*sect != &(file_format->getText())) &&
1717 (*sect != &(file_format->getData())) &&
1718 (*sect != &(file_format->getBSS())) &&
1719 (config().scripts().addressMap().find((*sect)->name()) !=
1720 config().scripts().addressMap().end()))
1721 // 4. create PT_LOAD for sections in address map except for text, data,
1722 // and bss
1723 createPT_LOAD = true;
1724 }
1725
1726 if (createPT_LOAD) {
1727 // create new PT_LOAD segment
1728 load_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_LOAD, cur_flag);
1729 if (!config().options().nmagic() && !config().options().omagic())
1730 load_seg->setAlign(abiPageSize());
1731 }
1732
1733 assert(NULL != load_seg);
1734 load_seg->addSection((*sect));
1735 if (cur_flag != prev_flag)
1736 load_seg->updateFlag(cur_flag);
1737
1738 prev_flag = cur_flag;
1739 }
1740
1741 // make PT_DYNAMIC
1742 if (file_format->hasDynamic()) {
1743 ELFSegment* dyn_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_DYNAMIC,
1744 llvm::ELF::PF_R |
1745 llvm::ELF::PF_W);
1746 dyn_seg->addSection(&file_format->getDynamic());
1747 }
1748
1749 if (config().options().hasRelro()) {
1750 // make PT_GNU_RELRO
1751 ELFSegment* relro_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_RELRO);
1752 for (ELFSegmentFactory::iterator seg = elfSegmentTable().begin(),
1753 segEnd = elfSegmentTable().end(); seg != segEnd; ++seg) {
1754 if (llvm::ELF::PT_LOAD != (*seg).type())
1755 continue;
1756
1757 for (ELFSegment::sect_iterator sect = (*seg).begin(),
1758 sectEnd = (*seg).end(); sect != sectEnd; ++sect) {
1759 unsigned int order = getSectionOrder(**sect);
1760 if (SHO_RELRO_LOCAL == order ||
1761 SHO_RELRO == order ||
1762 SHO_RELRO_LAST == order) {
1763 relro_seg->addSection(*sect);
1764 }
1765 }
1766 }
1767 }
1768
1769 // make PT_GNU_EH_FRAME
1770 if (file_format->hasEhFrameHdr()) {
1771 ELFSegment* eh_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_EH_FRAME);
1772 eh_seg->addSection(&file_format->getEhFrameHdr());
1773 }
1774
1775 // make PT_TLS
1776 if (file_format->hasTData() || file_format->hasTBSS()) {
1777 ELFSegment* tls_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_TLS);
1778 if (file_format->hasTData())
1779 tls_seg->addSection(&file_format->getTData());
1780 if (file_format->hasTBSS())
1781 tls_seg->addSection(&file_format->getTBSS());
1782 }
1783
1784 // make PT_GNU_STACK
1785 if (file_format->hasStackNote()) {
1786 m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_STACK,
1787 llvm::ELF::PF_R |
1788 llvm::ELF::PF_W |
1789 getSegmentFlag(file_format->getStackNote().flag()));
1790 }
1791
1792 // make PT_NOTE
1793 ELFSegment *note_seg = NULL;
1794 prev_flag = getSegmentFlag(0);
1795 for (sect = pModule.begin(); sect != sect_end; ++sect) {
1796 if ((*sect)->kind() != LDFileFormat::Note ||
1797 ((*sect)->flag() & llvm::ELF::SHF_ALLOC) == 0)
1798 continue;
1799
1800 cur_flag = getSegmentFlag((*sect)->flag());
1801 // we have different section orders for read-only and writable notes, so
1802 // create 2 segments if needed.
1803 if (note_seg == NULL ||
1804 (cur_flag & llvm::ELF::PF_W) != (prev_flag & llvm::ELF::PF_W))
1805 note_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_NOTE, cur_flag);
1806
1807 note_seg->addSection(*sect);
1808 prev_flag = cur_flag;
1809 }
1810
1811 // create target dependent segments
1812 doCreateProgramHdrs(pModule);
1813 }
1814
1815 /// setupProgramHdrs - set up the attributes of segments
setupProgramHdrs()1816 void GNULDBackend::setupProgramHdrs()
1817 {
1818 // update segment info
1819 ELFSegmentFactory::iterator seg, seg_end = m_ELFSegmentTable.end();
1820 for (seg = m_ELFSegmentTable.begin(); seg != seg_end; ++seg) {
1821 ELFSegment& segment = *seg;
1822
1823 // update PT_PHDR
1824 if (llvm::ELF::PT_PHDR == segment.type()) {
1825 uint64_t offset, phdr_size;
1826 if (config().targets().is32Bits()) {
1827 offset = sizeof(llvm::ELF::Elf32_Ehdr);
1828 phdr_size = sizeof(llvm::ELF::Elf32_Phdr);
1829 }
1830 else {
1831 offset = sizeof(llvm::ELF::Elf64_Ehdr);
1832 phdr_size = sizeof(llvm::ELF::Elf64_Phdr);
1833 }
1834 segment.setOffset(offset);
1835 segment.setVaddr(segmentStartAddr() + offset);
1836 segment.setPaddr(segment.vaddr());
1837 segment.setFilesz(numOfSegments() * phdr_size);
1838 segment.setMemsz(numOfSegments() * phdr_size);
1839 segment.setAlign(config().targets().bitclass() / 8);
1840 continue;
1841 }
1842
1843 // bypass if there is no section in this segment (e.g., PT_GNU_STACK)
1844 if (segment.numOfSections() == 0)
1845 continue;
1846
1847 segment.setOffset(segment.front()->offset());
1848 if (llvm::ELF::PT_LOAD == segment.type() &&
1849 LDFileFormat::Null == segment.front()->kind())
1850 segment.setVaddr(segmentStartAddr());
1851 else
1852 segment.setVaddr(segment.front()->addr());
1853 segment.setPaddr(segment.vaddr());
1854
1855 const LDSection* last_sect = segment.back();
1856 assert(NULL != last_sect);
1857 uint64_t file_size = last_sect->offset() - segment.offset();
1858 if (LDFileFormat::BSS != last_sect->kind())
1859 file_size += last_sect->size();
1860 segment.setFilesz(file_size);
1861
1862 segment.setMemsz(last_sect->addr() - segment.vaddr() + last_sect->size());
1863 }
1864 }
1865
1866 /// setupGNUStackInfo - setup the section flag of .note.GNU-stack in output
1867 /// @ref gold linker: layout.cc:2608
setupGNUStackInfo(Module & pModule)1868 void GNULDBackend::setupGNUStackInfo(Module& pModule)
1869 {
1870 uint32_t flag = 0x0;
1871 if (config().options().hasStackSet()) {
1872 // 1. check the command line option (-z execstack or -z noexecstack)
1873 if (config().options().hasExecStack())
1874 flag = llvm::ELF::SHF_EXECINSTR;
1875 }
1876 else {
1877 // 2. check the stack info from the input objects
1878 // FIXME: since we alway emit .note.GNU-stack in output now, we may be able
1879 // to check this from the output .note.GNU-stack directly after section
1880 // merging is done
1881 size_t object_count = 0, stack_note_count = 0;
1882 Module::const_obj_iterator obj, objEnd = pModule.obj_end();
1883 for (obj = pModule.obj_begin(); obj != objEnd; ++obj) {
1884 ++object_count;
1885 const LDSection* sect = (*obj)->context()->getSection(".note.GNU-stack");
1886 if (NULL != sect) {
1887 ++stack_note_count;
1888 // 2.1 found a stack note that is set as executable
1889 if (0 != (llvm::ELF::SHF_EXECINSTR & sect->flag())) {
1890 flag = llvm::ELF::SHF_EXECINSTR;
1891 break;
1892 }
1893 }
1894 }
1895
1896 // 2.2 there are no stack note sections in all input objects
1897 if (0 == stack_note_count)
1898 return;
1899
1900 // 2.3 a special case. Use the target default to decide if the stack should
1901 // be executable
1902 if (llvm::ELF::SHF_EXECINSTR != flag && object_count != stack_note_count)
1903 if (m_pInfo->isDefaultExecStack())
1904 flag = llvm::ELF::SHF_EXECINSTR;
1905 }
1906
1907 if (getOutputFormat()->hasStackNote()) {
1908 getOutputFormat()->getStackNote().setFlag(flag);
1909 }
1910 }
1911
1912 /// setupRelro - setup the offset constraint of PT_RELRO
setupRelro(Module & pModule)1913 void GNULDBackend::setupRelro(Module& pModule)
1914 {
1915 assert(config().options().hasRelro());
1916 // if -z relro is given, we need to adjust sections' offset again, and let
1917 // PT_GNU_RELRO end on a common page boundary
1918
1919 Module::iterator sect = pModule.begin();
1920 for (Module::iterator sect_end = pModule.end(); sect != sect_end; ++sect) {
1921 // find the first non-relro section
1922 if (getSectionOrder(**sect) > SHO_RELRO_LAST)
1923 break;
1924 }
1925
1926 // align the first non-relro section to page boundary
1927 uint64_t offset = (*sect)->offset();
1928 alignAddress(offset, commonPageSize());
1929 (*sect)->setOffset(offset);
1930
1931 // It seems that compiler think .got and .got.plt are continuous (w/o any
1932 // padding between). If .got is the last section in PT_RELRO and it's not
1933 // continuous to its next section (i.e. .got.plt), we need to add padding
1934 // in front of .got instead.
1935 // FIXME: Maybe we can handle this in a more general way.
1936 LDSection& got = getOutputFormat()->getGOT();
1937 if ((getSectionOrder(got) == SHO_RELRO_LAST) &&
1938 (got.offset() + got.size() != offset)) {
1939 got.setOffset(offset - got.size());
1940 }
1941
1942 // set up remaining section's offset
1943 setOutputSectionOffset(pModule, ++sect, pModule.end());
1944 }
1945
1946 /// setOutputSectionOffset - helper function to set a group of output sections'
1947 /// offset, and set pSectBegin to pStartOffset if pStartOffset is not -1U.
setOutputSectionOffset(Module & pModule,Module::iterator pSectBegin,Module::iterator pSectEnd,uint64_t pStartOffset)1948 void GNULDBackend::setOutputSectionOffset(Module& pModule,
1949 Module::iterator pSectBegin,
1950 Module::iterator pSectEnd,
1951 uint64_t pStartOffset)
1952 {
1953 if (pSectBegin == pModule.end())
1954 return;
1955
1956 assert(pSectEnd == pModule.end() ||
1957 (pSectEnd != pModule.end() &&
1958 (*pSectBegin)->index() <= (*pSectEnd)->index()));
1959
1960 if (pStartOffset != -1U) {
1961 (*pSectBegin)->setOffset(pStartOffset);
1962 ++pSectBegin;
1963 }
1964
1965 // set up the "cur" and "prev" iterator
1966 Module::iterator cur = pSectBegin;
1967 Module::iterator prev = pSectBegin;
1968 if (cur != pModule.begin())
1969 --prev;
1970 else
1971 ++cur;
1972
1973 for (; cur != pSectEnd; ++cur, ++prev) {
1974 uint64_t offset = 0x0;
1975 switch ((*prev)->kind()) {
1976 case LDFileFormat::Null:
1977 offset = sectionStartOffset();
1978 break;
1979 case LDFileFormat::BSS:
1980 offset = (*prev)->offset();
1981 break;
1982 default:
1983 offset = (*prev)->offset() + (*prev)->size();
1984 break;
1985 }
1986
1987 alignAddress(offset, (*cur)->align());
1988 (*cur)->setOffset(offset);
1989 }
1990 }
1991
1992 /// setOutputSectionOffset - helper function to set output sections' address
setOutputSectionAddress(Module & pModule,Module::iterator pSectBegin,Module::iterator pSectEnd)1993 void GNULDBackend::setOutputSectionAddress(Module& pModule,
1994 Module::iterator pSectBegin,
1995 Module::iterator pSectEnd)
1996 {
1997 if (pSectBegin == pModule.end())
1998 return;
1999
2000 assert(pSectEnd == pModule.end() ||
2001 (pSectEnd != pModule.end() &&
2002 (*pSectBegin)->index() <= (*pSectEnd)->index()));
2003
2004 for (ELFSegmentFactory::iterator seg = elfSegmentTable().begin(),
2005 segEnd = elfSegmentTable().end(), prev = elfSegmentTable().end();
2006 seg != segEnd; prev = seg, ++seg) {
2007 if (llvm::ELF::PT_LOAD != (*seg).type())
2008 continue;
2009
2010 uint64_t start_addr = 0x0;
2011 ScriptOptions::AddressMap::const_iterator mapping;
2012 if ((*seg).front()->kind() == LDFileFormat::Null)
2013 mapping = config().scripts().addressMap().find(".text");
2014 else if ((*seg).isDataSegment())
2015 mapping = config().scripts().addressMap().find(".data");
2016 else if ((*seg).isBssSegment())
2017 mapping = config().scripts().addressMap().find(".bss");
2018 else
2019 mapping = config().scripts().addressMap().find((*seg).front()->name());
2020
2021 if (mapping != config().scripts().addressMap().end()) {
2022 // use address mapping in script options
2023 start_addr = mapping.getEntry()->value();
2024 }
2025 else {
2026 if ((*seg).front()->kind() == LDFileFormat::Null) {
2027 // 1st PT_LOAD
2028 start_addr = segmentStartAddr();
2029 }
2030 else if ((*prev).front()->kind() == LDFileFormat::Null) {
2031 // prev segment is 1st PT_LOAD
2032 start_addr = segmentStartAddr() + (*seg).front()->offset();
2033 }
2034 else {
2035 // Others
2036 start_addr = (*prev).front()->addr() + (*seg).front()->offset();
2037 }
2038 // Try to align p_vaddr at page boundary if not in script options.
2039 // To do so will add more padding in file, but can save one page
2040 // at runtime.
2041 alignAddress(start_addr, (*seg).align());
2042 }
2043
2044 // in p75, http://www.sco.com/developers/devspecs/gabi41.pdf
2045 // p_align: As "Program Loading" describes in this chapter of the
2046 // processor supplement, loadable process segments must have congruent
2047 // values for p_vaddr and p_offset, modulo the page size.
2048 if ((start_addr & ((*seg).align() - 1)) !=
2049 ((*seg).front()->offset() & ((*seg).align() - 1))) {
2050 uint64_t padding = (*seg).align() +
2051 (start_addr & ((*seg).align() - 1)) -
2052 ((*seg).front()->offset() & ((*seg).align() - 1));
2053 setOutputSectionOffset(pModule,
2054 pModule.begin() + (*seg).front()->index(),
2055 pModule.end(),
2056 (*seg).front()->offset() + padding);
2057 if (config().options().hasRelro())
2058 setupRelro(pModule);
2059 }
2060
2061 for (ELFSegment::sect_iterator sect = (*seg).begin(),
2062 sectEnd = (*seg).end(); sect != sectEnd; ++sect) {
2063 if ((*sect)->index() < (*pSectBegin)->index())
2064 continue;
2065
2066 if (LDFileFormat::Null == (*sect)->kind())
2067 continue;
2068
2069 if (sect == pSectEnd)
2070 return;
2071
2072 if (sect != (*seg).begin())
2073 (*sect)->setAddr(start_addr + (*sect)->offset() -
2074 (*seg).front()->offset());
2075 else
2076 (*sect)->setAddr(start_addr);
2077 }
2078 }
2079 }
2080
2081 /// layout - layout method
layout(Module & pModule)2082 void GNULDBackend::layout(Module& pModule)
2083 {
2084 std::vector<SHOEntry> output_list;
2085 // 1. determine what sections will go into final output, and push the needed
2086 // sections into output_list for later processing
2087 for (Module::iterator it = pModule.begin(), ie = pModule.end(); it != ie;
2088 ++it) {
2089 switch ((*it)->kind()) {
2090 // take NULL and StackNote directly
2091 case LDFileFormat::Null:
2092 case LDFileFormat::StackNote:
2093 output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
2094 break;
2095 // ignore if section size is 0
2096 case LDFileFormat::EhFrame:
2097 if (((*it)->size() != 0) ||
2098 ((*it)->hasEhFrame() &&
2099 config().codeGenType() == LinkerConfig::Object))
2100 output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
2101 break;
2102 case LDFileFormat::Relocation:
2103 if (((*it)->size() != 0) ||
2104 ((*it)->hasRelocData() &&
2105 config().codeGenType() == LinkerConfig::Object))
2106 output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
2107 break;
2108 case LDFileFormat::Regular:
2109 case LDFileFormat::Target:
2110 case LDFileFormat::MetaData:
2111 case LDFileFormat::BSS:
2112 case LDFileFormat::Debug:
2113 case LDFileFormat::GCCExceptTable:
2114 case LDFileFormat::Note:
2115 case LDFileFormat::NamePool:
2116 case LDFileFormat::EhFrameHdr:
2117 if (((*it)->size() != 0) ||
2118 ((*it)->hasSectionData() &&
2119 config().codeGenType() == LinkerConfig::Object))
2120 output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
2121 break;
2122 case LDFileFormat::Group:
2123 if (LinkerConfig::Object == config().codeGenType()) {
2124 //TODO: support incremental linking
2125 ;
2126 }
2127 break;
2128 case LDFileFormat::Version:
2129 if (0 != (*it)->size()) {
2130 output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
2131 warning(diag::warn_unsupported_symbolic_versioning) << (*it)->name();
2132 }
2133 break;
2134 default:
2135 if (0 != (*it)->size()) {
2136 error(diag::err_unsupported_section) << (*it)->name() << (*it)->kind();
2137 }
2138 break;
2139 }
2140 } // end of for
2141
2142 // 2. sort output section orders
2143 std::stable_sort(output_list.begin(), output_list.end(), SHOCompare());
2144
2145 // 3. update output sections in Module
2146 pModule.getSectionTable().clear();
2147 for(size_t index = 0; index < output_list.size(); ++index) {
2148 (output_list[index].first)->setIndex(index);
2149 pModule.getSectionTable().push_back(output_list[index].first);
2150 }
2151
2152 // 4. create program headers
2153 if (LinkerConfig::Object != config().codeGenType()) {
2154 createProgramHdrs(pModule);
2155 }
2156
2157 // 5. set output section offset
2158 setOutputSectionOffset(pModule, pModule.begin(), pModule.end(), 0x0);
2159 }
2160
2161 /// preLayout - Backend can do any needed modification before layout
preLayout(Module & pModule,IRBuilder & pBuilder)2162 void GNULDBackend::preLayout(Module& pModule, IRBuilder& pBuilder)
2163 {
2164 // prelayout target first
2165 doPreLayout(pBuilder);
2166
2167 if (LinkerConfig::Object != config().codeGenType() &&
2168 config().options().hasEhFrameHdr() && getOutputFormat()->hasEhFrame()) {
2169 // init EhFrameHdr and size the output section
2170 ELFFileFormat* format = getOutputFormat();
2171 m_pEhFrameHdr = new EhFrameHdr(format->getEhFrameHdr(),
2172 format->getEhFrame());
2173 m_pEhFrameHdr->sizeOutput();
2174 }
2175
2176 // change .tbss and .tdata section symbol from Local to LocalDyn category
2177 if (NULL != f_pTDATA)
2178 pModule.getSymbolTable().changeLocalToDynamic(*f_pTDATA);
2179
2180 if (NULL != f_pTBSS)
2181 pModule.getSymbolTable().changeLocalToDynamic(*f_pTBSS);
2182
2183 // To merge input's relocation sections into output's relocation sections.
2184 //
2185 // If we are generating relocatables (-r), move input relocation sections
2186 // to corresponding output relocation sections.
2187 if (LinkerConfig::Object == config().codeGenType()) {
2188 Module::obj_iterator input, inEnd = pModule.obj_end();
2189 for (input = pModule.obj_begin(); input != inEnd; ++input) {
2190 LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
2191 for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
2192
2193 // get the output relocation LDSection with identical name.
2194 LDSection* output_sect = pModule.getSection((*rs)->name());
2195 if (NULL == output_sect) {
2196 output_sect = LDSection::Create((*rs)->name(),
2197 (*rs)->kind(),
2198 (*rs)->type(),
2199 (*rs)->flag());
2200
2201 output_sect->setAlign((*rs)->align());
2202 pModule.getSectionTable().push_back(output_sect);
2203 }
2204
2205 // set output relocation section link
2206 const LDSection* input_link = (*rs)->getLink();
2207 assert(NULL != input_link && "Illegal input relocation section.");
2208
2209 // get the linked output section
2210 LDSection* output_link = pModule.getSection(input_link->name());
2211 assert(NULL != output_link);
2212
2213 output_sect->setLink(output_link);
2214
2215 // get output relcoationData, create one if not exist
2216 if (!output_sect->hasRelocData())
2217 IRBuilder::CreateRelocData(*output_sect);
2218
2219 RelocData* out_reloc_data = output_sect->getRelocData();
2220
2221 // move relocations from input's to output's RelcoationData
2222 RelocData::RelocationListType& out_list =
2223 out_reloc_data->getRelocationList();
2224 RelocData::RelocationListType& in_list =
2225 (*rs)->getRelocData()->getRelocationList();
2226 out_list.splice(out_list.end(), in_list);
2227
2228 // size output
2229 if (llvm::ELF::SHT_REL == output_sect->type())
2230 output_sect->setSize(out_reloc_data->size() * getRelEntrySize());
2231 else if (llvm::ELF::SHT_RELA == output_sect->type())
2232 output_sect->setSize(out_reloc_data->size() * getRelaEntrySize());
2233 else {
2234 fatal(diag::unknown_reloc_section_type) << output_sect->type()
2235 << output_sect->name();
2236 }
2237 } // end of for each relocation section
2238 } // end of for each input
2239 } // end of if
2240
2241 // set up the section flag of .note.GNU-stack section
2242 setupGNUStackInfo(pModule);
2243 }
2244
2245 /// postLayout - Backend can do any needed modification after layout
postLayout(Module & pModule,IRBuilder & pBuilder)2246 void GNULDBackend::postLayout(Module& pModule, IRBuilder& pBuilder)
2247 {
2248 // 1. set up section address and segment attributes
2249 if (LinkerConfig::Object != config().codeGenType()) {
2250 if (config().options().hasRelro()) {
2251 // 1.1 set up the offset constraint of PT_RELRO
2252 setupRelro(pModule);
2253 }
2254
2255 // 1.2 set up the output sections' address
2256 setOutputSectionAddress(pModule, pModule.begin(), pModule.end());
2257
2258 // 1.3 do relaxation
2259 relax(pModule, pBuilder);
2260
2261 // 1.4 set up the attributes of program headers
2262 setupProgramHdrs();
2263 }
2264
2265 // 2. target specific post layout
2266 doPostLayout(pModule, pBuilder);
2267 }
2268
postProcessing(MemoryArea & pOutput)2269 void GNULDBackend::postProcessing(MemoryArea& pOutput)
2270 {
2271 if (LinkerConfig::Object != config().codeGenType() &&
2272 config().options().hasEhFrameHdr() && getOutputFormat()->hasEhFrame()) {
2273 // emit eh_frame_hdr
2274 if (config().targets().is32Bits())
2275 m_pEhFrameHdr->emitOutput<32>(pOutput);
2276 else
2277 m_pEhFrameHdr->emitOutput<64>(pOutput);
2278 }
2279 }
2280
2281 /// getHashBucketCount - calculate hash bucket count.
2282 /// @ref Google gold linker, dynobj.cc:791
getHashBucketCount(unsigned pNumOfSymbols,bool pIsGNUStyle)2283 unsigned GNULDBackend::getHashBucketCount(unsigned pNumOfSymbols,
2284 bool pIsGNUStyle)
2285 {
2286 // @ref Google gold, dynobj.cc:loc 791
2287 static const unsigned int buckets[] =
2288 {
2289 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2290 16411, 32771, 65537, 131101, 262147
2291 };
2292 const unsigned buckets_count = sizeof buckets / sizeof buckets[0];
2293
2294 unsigned int result = 1;
2295 for (unsigned i = 0; i < buckets_count; ++i) {
2296 if (pNumOfSymbols < buckets[i])
2297 break;
2298 result = buckets[i];
2299 }
2300
2301 if (pIsGNUStyle && result < 2)
2302 result = 2;
2303
2304 return result;
2305 }
2306
2307 /// getGNUHashMaskbitslog2 - calculate the number of mask bits in log2
2308 /// @ref binutils gold, dynobj.cc:1165
getGNUHashMaskbitslog2(unsigned pNumOfSymbols) const2309 unsigned GNULDBackend::getGNUHashMaskbitslog2(unsigned pNumOfSymbols) const
2310 {
2311 uint32_t maskbitslog2 = 1;
2312 for (uint32_t x = pNumOfSymbols >> 1; x != 0; x >>=1)
2313 ++maskbitslog2;
2314
2315 if (maskbitslog2 < 3)
2316 maskbitslog2 = 5;
2317 else if (((1U << (maskbitslog2 - 2)) & pNumOfSymbols) != 0)
2318 maskbitslog2 += 3;
2319 else
2320 maskbitslog2 += 2;
2321
2322 if (config().targets().bitclass() == 64 && maskbitslog2 == 5)
2323 maskbitslog2 = 6;
2324
2325 return maskbitslog2;
2326 }
2327
2328 /// isDynamicSymbol
2329 /// @ref Google gold linker: symtab.cc:311
isDynamicSymbol(const LDSymbol & pSymbol)2330 bool GNULDBackend::isDynamicSymbol(const LDSymbol& pSymbol)
2331 {
2332 // If a local symbol is in the LDContext's symbol table, it's a real local
2333 // symbol. We should not add it
2334 if (pSymbol.binding() == ResolveInfo::Local)
2335 return false;
2336
2337 // If we are building shared object, and the visibility is external, we
2338 // need to add it.
2339 if (LinkerConfig::DynObj == config().codeGenType() ||
2340 LinkerConfig::Exec == config().codeGenType() ||
2341 LinkerConfig::Binary == config().codeGenType()) {
2342 if (pSymbol.resolveInfo()->visibility() == ResolveInfo::Default ||
2343 pSymbol.resolveInfo()->visibility() == ResolveInfo::Protected)
2344 return true;
2345 }
2346 return false;
2347 }
2348
2349 /// isDynamicSymbol
2350 /// @ref Google gold linker: symtab.cc:311
isDynamicSymbol(const ResolveInfo & pResolveInfo)2351 bool GNULDBackend::isDynamicSymbol(const ResolveInfo& pResolveInfo)
2352 {
2353 // If a local symbol is in the LDContext's symbol table, it's a real local
2354 // symbol. We should not add it
2355 if (pResolveInfo.binding() == ResolveInfo::Local)
2356 return false;
2357
2358 // If we are building shared object, and the visibility is external, we
2359 // need to add it.
2360 if (LinkerConfig::DynObj == config().codeGenType() ||
2361 LinkerConfig::Exec == config().codeGenType() ||
2362 LinkerConfig::Binary == config().codeGenType()) {
2363 if (pResolveInfo.visibility() == ResolveInfo::Default ||
2364 pResolveInfo.visibility() == ResolveInfo::Protected)
2365 return true;
2366 }
2367 return false;
2368 }
2369
2370 /// commonPageSize - the common page size of the target machine.
2371 /// @ref gold linker: target.h:135
commonPageSize() const2372 uint64_t GNULDBackend::commonPageSize() const
2373 {
2374 if (config().options().commPageSize() > 0)
2375 return std::min(config().options().commPageSize(), abiPageSize());
2376 else
2377 return std::min(m_pInfo->commonPageSize(), abiPageSize());
2378 }
2379
2380 /// abiPageSize - the abi page size of the target machine.
2381 /// @ref gold linker: target.h:125
abiPageSize() const2382 uint64_t GNULDBackend::abiPageSize() const
2383 {
2384 if (config().options().maxPageSize() > 0)
2385 return config().options().maxPageSize();
2386 else
2387 return m_pInfo->abiPageSize();
2388 }
2389
2390 /// isSymbolPreemtible - whether the symbol can be preemted by other
2391 /// link unit
2392 /// @ref Google gold linker, symtab.h:551
isSymbolPreemptible(const ResolveInfo & pSym) const2393 bool GNULDBackend::isSymbolPreemptible(const ResolveInfo& pSym) const
2394 {
2395 if (pSym.other() != ResolveInfo::Default)
2396 return false;
2397
2398 // This is because the codeGenType of pie is DynObj. And gold linker check
2399 // the "shared" option instead.
2400 if (config().options().isPIE())
2401 return false;
2402
2403 if (LinkerConfig::DynObj != config().codeGenType())
2404 return false;
2405
2406 if (config().options().Bsymbolic())
2407 return false;
2408
2409 // A local defined symbol should be non-preemptible.
2410 // This issue is found when linking libstdc++ on freebsd. A R_386_GOT32
2411 // relocation refers to a local defined symbol, and we should generate a
2412 // relative dynamic relocation when applying the relocation.
2413 if (pSym.isDefine() && pSym.binding() == ResolveInfo::Local)
2414 return false;
2415
2416 return true;
2417 }
2418
2419 /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation
2420 /// @ref Google gold linker, symtab.h:645
symbolNeedsDynRel(const ResolveInfo & pSym,bool pSymHasPLT,bool isAbsReloc) const2421 bool GNULDBackend::symbolNeedsDynRel(const ResolveInfo& pSym,
2422 bool pSymHasPLT,
2423 bool isAbsReloc) const
2424 {
2425 // an undefined reference in the executables should be statically
2426 // resolved to 0 and no need a dynamic relocation
2427 if (pSym.isUndef() &&
2428 !pSym.isDyn() &&
2429 (LinkerConfig::Exec == config().codeGenType() ||
2430 LinkerConfig::Binary == config().codeGenType()))
2431 return false;
2432
2433 // An absolute symbol can be resolved directly if it is either local
2434 // or we are linking statically. Otherwise it can still be overridden
2435 // at runtime.
2436 if (pSym.isAbsolute() &&
2437 (pSym.binding() == ResolveInfo::Local || config().isCodeStatic()))
2438 return false;
2439 if (config().isCodeIndep() && isAbsReloc)
2440 return true;
2441 if (pSymHasPLT && ResolveInfo::Function == pSym.type())
2442 return false;
2443 if (!config().isCodeIndep() && pSymHasPLT)
2444 return false;
2445 if (pSym.isDyn() || pSym.isUndef() ||
2446 isSymbolPreemptible(pSym))
2447 return true;
2448
2449 return false;
2450 }
2451
2452 /// symbolNeedsPLT - return whether the symbol needs a PLT entry
2453 /// @ref Google gold linker, symtab.h:596
symbolNeedsPLT(const ResolveInfo & pSym) const2454 bool GNULDBackend::symbolNeedsPLT(const ResolveInfo& pSym) const
2455 {
2456 if (pSym.isUndef() &&
2457 !pSym.isDyn() &&
2458 LinkerConfig::DynObj != config().codeGenType())
2459 return false;
2460
2461 // An IndirectFunc symbol (i.e., STT_GNU_IFUNC) always needs a plt entry
2462 if (pSym.type() == ResolveInfo::IndirectFunc)
2463 return true;
2464
2465 if (pSym.type() != ResolveInfo::Function)
2466 return false;
2467
2468 if (config().isCodeStatic())
2469 return false;
2470
2471 if (config().options().isPIE())
2472 return false;
2473
2474 return (pSym.isDyn() ||
2475 pSym.isUndef() ||
2476 isSymbolPreemptible(pSym));
2477 }
2478
2479 /// symbolHasFinalValue - return true if the symbol's value can be decided at
2480 /// link time
2481 /// @ref Google gold linker, Symbol::final_value_is_known
symbolFinalValueIsKnown(const ResolveInfo & pSym) const2482 bool GNULDBackend::symbolFinalValueIsKnown(const ResolveInfo& pSym) const
2483 {
2484 // if the output is pic code or if not executables, symbols' value may change
2485 // at runtime
2486 // FIXME: CodeIndep() || LinkerConfig::Relocatable == CodeGenType
2487 if (config().isCodeIndep() ||
2488 (LinkerConfig::Exec != config().codeGenType() &&
2489 LinkerConfig::Binary != config().codeGenType()))
2490 return false;
2491
2492 // if the symbol is from dynamic object, then its value is unknown
2493 if (pSym.isDyn())
2494 return false;
2495
2496 // if the symbol is not in dynamic object and is not undefined, then its value
2497 // is known
2498 if (!pSym.isUndef())
2499 return true;
2500
2501 // if the symbol is undefined and not in dynamic objects, for example, a weak
2502 // undefined symbol, then whether the symbol's final value can be known
2503 // depends on whrther we're doing static link
2504 return config().isCodeStatic();
2505 }
2506
2507 /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation
symbolNeedsCopyReloc(const Relocation & pReloc,const ResolveInfo & pSym) const2508 bool GNULDBackend::symbolNeedsCopyReloc(const Relocation& pReloc,
2509 const ResolveInfo& pSym) const
2510 {
2511 // only the reference from dynamic executable to non-function symbol in
2512 // the dynamic objects may need copy relocation
2513 if (config().isCodeIndep() ||
2514 !pSym.isDyn() ||
2515 pSym.type() == ResolveInfo::Function ||
2516 pSym.size() == 0)
2517 return false;
2518
2519 // check if the option -z nocopyreloc is given
2520 if (config().options().hasNoCopyReloc())
2521 return false;
2522
2523 // TODO: Is this check necessary?
2524 // if relocation target place is readonly, a copy relocation is needed
2525 uint32_t flag = pReloc.targetRef().frag()->getParent()->getSection().flag();
2526 if (0 == (flag & llvm::ELF::SHF_WRITE))
2527 return true;
2528
2529 return false;
2530 }
2531
getTDATASymbol()2532 LDSymbol& GNULDBackend::getTDATASymbol()
2533 {
2534 assert(NULL != f_pTDATA);
2535 return *f_pTDATA;
2536 }
2537
getTDATASymbol() const2538 const LDSymbol& GNULDBackend::getTDATASymbol() const
2539 {
2540 assert(NULL != f_pTDATA);
2541 return *f_pTDATA;
2542 }
2543
getTBSSSymbol()2544 LDSymbol& GNULDBackend::getTBSSSymbol()
2545 {
2546 assert(NULL != f_pTBSS);
2547 return *f_pTBSS;
2548 }
2549
getTBSSSymbol() const2550 const LDSymbol& GNULDBackend::getTBSSSymbol() const
2551 {
2552 assert(NULL != f_pTBSS);
2553 return *f_pTBSS;
2554 }
2555
checkAndSetHasTextRel(const LDSection & pSection)2556 void GNULDBackend::checkAndSetHasTextRel(const LDSection& pSection)
2557 {
2558 if (m_bHasTextRel)
2559 return;
2560
2561 // if the target section of the dynamic relocation is ALLOCATE but is not
2562 // writable, than we should set DF_TEXTREL
2563 const uint32_t flag = pSection.flag();
2564 if (0 == (flag & llvm::ELF::SHF_WRITE) && (flag & llvm::ELF::SHF_ALLOC))
2565 m_bHasTextRel = true;
2566
2567 return;
2568 }
2569
2570 /// initBRIslandFactory - initialize the branch island factory for relaxation
initBRIslandFactory()2571 bool GNULDBackend::initBRIslandFactory()
2572 {
2573 if (NULL == m_pBRIslandFactory) {
2574 m_pBRIslandFactory = new BranchIslandFactory(maxBranchOffset());
2575 }
2576 return true;
2577 }
2578
2579 /// initStubFactory - initialize the stub factory for relaxation
initStubFactory()2580 bool GNULDBackend::initStubFactory()
2581 {
2582 if (NULL == m_pStubFactory) {
2583 m_pStubFactory = new StubFactory();
2584 }
2585 return true;
2586 }
2587
relax(Module & pModule,IRBuilder & pBuilder)2588 bool GNULDBackend::relax(Module& pModule, IRBuilder& pBuilder)
2589 {
2590 if (!mayRelax())
2591 return true;
2592
2593 bool finished = true;
2594 do {
2595 if (doRelax(pModule, pBuilder, finished)) {
2596 // If the sections (e.g., .text) are relaxed, the layout is also changed
2597 // We need to do the following:
2598
2599 // 1. set up the offset
2600 setOutputSectionOffset(pModule, pModule.begin(), pModule.end());
2601
2602 // 2. set up the offset constraint of PT_RELRO
2603 if (config().options().hasRelro())
2604 setupRelro(pModule);
2605
2606 // 3. set up the output sections' address
2607 setOutputSectionAddress(pModule, pModule.begin(), pModule.end());
2608 }
2609 } while (!finished);
2610
2611 return true;
2612 }
2613
needGNUHash(const LDSymbol & X) const2614 bool GNULDBackend::DynsymCompare::needGNUHash(const LDSymbol& X) const
2615 {
2616 // FIXME: in bfd and gold linker, an undefined symbol might be hashed
2617 // when the ouput is not PIC, if the symbol is referred by a non pc-relative
2618 // reloc, and its value is set to the addr of the plt entry.
2619 return !X.resolveInfo()->isUndef() && !X.isDyn();
2620 }
2621
operator ()(const LDSymbol * X,const LDSymbol * Y) const2622 bool GNULDBackend::DynsymCompare::operator()(const LDSymbol* X,
2623 const LDSymbol* Y) const
2624 {
2625 return !needGNUHash(*X) && needGNUHash(*Y);
2626 }
2627
2628