• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ELFReader.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/LD/ELFReader.h>
10 
11 #include <mcld/IRBuilder.h>
12 #include <mcld/Fragment/FillFragment.h>
13 #include <mcld/LD/EhFrame.h>
14 #include <mcld/LD/SectionData.h>
15 #include <mcld/Target/GNULDBackend.h>
16 #include <mcld/Support/MemoryRegion.h>
17 #include <mcld/Support/MsgHandling.h>
18 #include <mcld/Object/ObjectBuilder.h>
19 
20 #include <cstring>
21 
22 #include <llvm/ADT/StringRef.h>
23 #include <llvm/ADT/Twine.h>
24 #include <llvm/Support/ELF.h>
25 #include <llvm/Support/Host.h>
26 
27 #include <iostream>
28 
29 using namespace mcld;
30 
31 //===----------------------------------------------------------------------===//
32 // ELFReader<32, true>
33 //===----------------------------------------------------------------------===//
34 /// constructor
ELFReader(GNULDBackend & pBackend)35 ELFReader<32, true>::ELFReader(GNULDBackend& pBackend)
36   : ELFReaderIF(pBackend) {
37 }
38 
39 /// destructor
~ELFReader()40 ELFReader<32, true>::~ELFReader()
41 {
42 }
43 
44 /// isELF - is this a ELF file
isELF(void * pELFHeader) const45 bool ELFReader<32, true>::isELF(void* pELFHeader) const
46 {
47   llvm::ELF::Elf32_Ehdr* hdr =
48                           reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader);
49   if (0 == memcmp(llvm::ELF::ElfMagic, hdr, 4))
50     return true;
51   return false;
52 }
53 
54 /// readRegularSection - read a regular section and create fragments.
55 bool
readRegularSection(Input & pInput,SectionData & pSD) const56 ELFReader<32, true>::readRegularSection(Input& pInput, SectionData& pSD) const
57 {
58   uint32_t offset = pInput.fileOffset() + pSD.getSection().offset();
59   uint32_t size = pSD.getSection().size();
60 
61   Fragment* frag = IRBuilder::CreateRegion(pInput, offset, size);
62   ObjectBuilder::AppendFragment(*frag, pSD);
63   return true;
64 }
65 
66 /// readSymbols - read ELF symbols and create LDSymbol
readSymbols(Input & pInput,IRBuilder & pBuilder,const MemoryRegion & pRegion,const char * pStrTab) const67 bool ELFReader<32, true>::readSymbols(Input& pInput,
68                                       IRBuilder& pBuilder,
69                                       const MemoryRegion& pRegion,
70                                       const char* pStrTab) const
71 {
72   // get number of symbols
73   size_t entsize = pRegion.size()/sizeof(llvm::ELF::Elf32_Sym);
74   const llvm::ELF::Elf32_Sym* symtab =
75                  reinterpret_cast<const llvm::ELF::Elf32_Sym*>(pRegion.start());
76 
77   uint32_t st_name  = 0x0;
78   uint32_t st_value = 0x0;
79   uint32_t st_size  = 0x0;
80   uint8_t  st_info  = 0x0;
81   uint8_t  st_other = 0x0;
82   uint16_t st_shndx = 0x0;
83 
84   // skip the first NULL symbol
85   pInput.context()->addSymbol(LDSymbol::Null());
86 
87   /// recording symbols added from DynObj to analyze weak alias
88   std::vector<AliasInfo> potential_aliases;
89   bool is_dyn_obj = (pInput.type()==Input::DynObj);
90   for (size_t idx = 1; idx < entsize; ++idx) {
91     st_info  = symtab[idx].st_info;
92     st_other = symtab[idx].st_other;
93 
94     if (llvm::sys::IsLittleEndianHost) {
95       st_name  = symtab[idx].st_name;
96       st_value = symtab[idx].st_value;
97       st_size  = symtab[idx].st_size;
98       st_shndx = symtab[idx].st_shndx;
99     }
100     else {
101       st_name  = mcld::bswap32(symtab[idx].st_name);
102       st_value = mcld::bswap32(symtab[idx].st_value);
103       st_size  = mcld::bswap32(symtab[idx].st_size);
104       st_shndx = mcld::bswap16(symtab[idx].st_shndx);
105     }
106 
107     // If the section should not be included, set the st_shndx SHN_UNDEF
108     // - A section in interrelated groups are not included.
109     if (pInput.type() == Input::Object &&
110         st_shndx < llvm::ELF::SHN_LORESERVE &&
111         st_shndx != llvm::ELF::SHN_UNDEF) {
112       if (NULL == pInput.context()->getSection(st_shndx))
113         st_shndx = llvm::ELF::SHN_UNDEF;
114     }
115 
116     // get ld_type
117     ResolveInfo::Type ld_type = getSymType(st_info, st_shndx);
118 
119     // get ld_desc
120     ResolveInfo::Desc ld_desc = getSymDesc(st_shndx, pInput);
121 
122     // get ld_binding
123     ResolveInfo::Binding ld_binding = getSymBinding((st_info >> 4), st_shndx, st_other);
124 
125     // get ld_value - ld_value must be section relative.
126     uint64_t ld_value = getSymValue(st_value, st_shndx, pInput);
127 
128     // get ld_vis
129     ResolveInfo::Visibility ld_vis = getSymVisibility(st_other);
130 
131     // get section
132     LDSection* section = NULL;
133     if (st_shndx < llvm::ELF::SHN_LORESERVE) // including ABS and COMMON
134       section = pInput.context()->getSection(st_shndx);
135 
136     // get ld_name
137     std::string ld_name;
138     if (ResolveInfo::Section == ld_type) {
139       // Section symbol's st_name is the section index.
140       assert(NULL != section && "get a invalid section");
141       ld_name = section->name();
142     }
143     else {
144       ld_name = std::string(pStrTab + st_name);
145     }
146 
147     LDSymbol *psym =
148         pBuilder.AddSymbol(pInput,
149                            ld_name,
150                            ld_type,
151                            ld_desc,
152                            ld_binding,
153                            st_size,
154                            ld_value,
155                            section, ld_vis);
156 
157     if ( is_dyn_obj
158          && NULL!=psym
159          && ResolveInfo::Undefined!=ld_desc
160          && (ResolveInfo::Global==ld_binding ||
161              ResolveInfo::Weak==ld_binding)
162          && ResolveInfo::Object==ld_type ) {
163       AliasInfo p;
164       p.pt_alias = psym;
165       p.ld_binding = ld_binding;
166       p.ld_value = ld_value;
167       potential_aliases.push_back(p);
168     }
169 
170   } // end of for loop
171 
172   // analyze weak alias
173   // FIXME: it is better to let IRBuilder handle alias anlysis.
174   //        1. eliminate code duplication
175   //        2. easy to know if a symbol is from .so
176   //           (so that it may be a potential alias)
177   if ( is_dyn_obj ) {
178     // sort symbols by symbol value and then weak before strong
179     std::sort( potential_aliases.begin(), potential_aliases.end(), less);
180 
181     // for each weak symbol, find out all its aliases, and
182     // then link them as a circular list in Module
183     std::vector<AliasInfo>::iterator sym_it, sym_e;
184     sym_e = potential_aliases.end();
185     for (sym_it = potential_aliases.begin(); sym_it!=sym_e; ++sym_it) {
186       if (ResolveInfo::Weak!=sym_it->ld_binding)
187         continue;
188 
189       Module& pModule = pBuilder.getModule();
190       std::vector<AliasInfo>::iterator alias_it = sym_it+1;
191       while(alias_it!=sym_e) {
192         if ( sym_it->ld_value != alias_it->ld_value )
193           break;
194 
195         if (sym_it+1==alias_it)
196           pModule.CreateAliasList(*sym_it->pt_alias->resolveInfo());
197         pModule.addAlias(*alias_it->pt_alias->resolveInfo());
198         ++alias_it;
199       }
200 
201       sym_it = alias_it-1;
202     }// end of for loop
203   }
204 
205   return true;
206 }
207 
208 //===----------------------------------------------------------------------===//
209 // ELFReader::read relocations - read ELF rela and rel, and create Relocation
210 //===----------------------------------------------------------------------===//
211 /// ELFReader::readRela - read ELF rela and create Relocation
readRela(Input & pInput,LDSection & pSection,const MemoryRegion & pRegion) const212 bool ELFReader<32, true>::readRela(Input& pInput,
213                                    LDSection& pSection,
214                                    const MemoryRegion& pRegion) const
215 {
216   // get the number of rela
217   size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf32_Rela);
218   const llvm::ELF::Elf32_Rela* relaTab =
219                 reinterpret_cast<const llvm::ELF::Elf32_Rela*>(pRegion.start());
220 
221   for (size_t idx=0; idx < entsize; ++idx) {
222     uint32_t r_offset = 0x0;
223     uint32_t r_info   = 0x0;
224     int32_t  r_addend = 0;
225     if (llvm::sys::IsLittleEndianHost) {
226       r_offset = relaTab[idx].r_offset;
227       r_info   = relaTab[idx].r_info;
228       r_addend = relaTab[idx].r_addend;
229     }
230     else {
231       r_offset = mcld::bswap32(relaTab[idx].r_offset);
232       r_info   = mcld::bswap32(relaTab[idx].r_info);
233       r_addend = mcld::bswap32(relaTab[idx].r_addend);
234     }
235 
236     uint8_t  r_type = static_cast<unsigned char>(r_info);
237     uint32_t r_sym  = (r_info >> 8);
238     LDSymbol* symbol = pInput.context()->getSymbol(r_sym);
239     if (NULL == symbol) {
240       fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path();
241     }
242 
243     IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset, r_addend);
244   } // end of for
245   return true;
246 }
247 
248 /// readRel - read ELF rel and create Relocation
readRel(Input & pInput,LDSection & pSection,const MemoryRegion & pRegion) const249 bool ELFReader<32, true>::readRel(Input& pInput,
250                                   LDSection& pSection,
251                                   const MemoryRegion& pRegion) const
252 {
253   // get the number of rel
254   size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf32_Rel);
255   const llvm::ELF::Elf32_Rel* relTab =
256                  reinterpret_cast<const llvm::ELF::Elf32_Rel*>(pRegion.start());
257 
258   for (size_t idx=0; idx < entsize; ++idx) {
259     uint32_t r_offset = 0x0;
260     uint32_t r_info   = 0x0;
261     if (llvm::sys::IsLittleEndianHost) {
262       r_offset = relTab[idx].r_offset;
263       r_info   = relTab[idx].r_info;
264     }
265     else {
266       r_offset = mcld::bswap32(relTab[idx].r_offset);
267       r_info   = mcld::bswap32(relTab[idx].r_info);
268     }
269 
270     uint8_t  r_type = static_cast<unsigned char>(r_info);
271     uint32_t r_sym  = (r_info >> 8);
272 
273     LDSymbol* symbol = pInput.context()->getSymbol(r_sym);
274     if (NULL == symbol) {
275       fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path();
276     }
277 
278     IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset);
279   } // end of for
280   return true;
281 }
282 
283 /// isMyEndian - is this ELF file in the same endian to me?
isMyEndian(void * pELFHeader) const284 bool ELFReader<32, true>::isMyEndian(void* pELFHeader) const
285 {
286   llvm::ELF::Elf32_Ehdr* hdr =
287                           reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader);
288 
289   return (hdr->e_ident[llvm::ELF::EI_DATA] == llvm::ELF::ELFDATA2LSB);
290 }
291 
292 /// isMyMachine - is this ELF file generated for the same machine.
isMyMachine(void * pELFHeader) const293 bool ELFReader<32, true>::isMyMachine(void* pELFHeader) const
294 {
295   llvm::ELF::Elf32_Ehdr* hdr =
296                           reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader);
297 
298   if (llvm::sys::IsLittleEndianHost)
299     return (hdr->e_machine == target().getInfo().machine());
300   return (mcld::bswap16(hdr->e_machine) == target().getInfo().machine());
301 }
302 
303 /// fileType - return the file type
fileType(void * pELFHeader) const304 Input::Type ELFReader<32, true>::fileType(void* pELFHeader) const
305 {
306   llvm::ELF::Elf32_Ehdr* hdr =
307                           reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader);
308   uint32_t type = 0x0;
309   if (llvm::sys::IsLittleEndianHost)
310     type = hdr->e_type;
311   else
312     type = mcld::bswap16(hdr->e_type);
313 
314   switch(type) {
315   case llvm::ELF::ET_REL:
316     return Input::Object;
317   case llvm::ELF::ET_EXEC:
318     return Input::Exec;
319   case llvm::ELF::ET_DYN:
320     return Input::DynObj;
321   case llvm::ELF::ET_CORE:
322     return Input::CoreFile;
323   case llvm::ELF::ET_NONE:
324   default:
325     return Input::Unknown;
326   }
327 }
328 
329 /// readSectionHeaders - read ELF section header table and create LDSections
330 bool
readSectionHeaders(Input & pInput,void * pELFHeader) const331 ELFReader<32, true>::readSectionHeaders(Input& pInput, void* pELFHeader) const
332 {
333   llvm::ELF::Elf32_Ehdr* ehdr =
334                           reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader);
335 
336   uint32_t shoff     = 0x0;
337   uint16_t shentsize = 0x0;
338   uint32_t shnum     = 0x0;
339   uint32_t shstrtab  = 0x0;
340 
341   if (llvm::sys::IsLittleEndianHost) {
342     shoff     = ehdr->e_shoff;
343     shentsize = ehdr->e_shentsize;
344     shnum     = ehdr->e_shnum;
345     shstrtab  = ehdr->e_shstrndx;
346   }
347   else {
348     shoff     = mcld::bswap32(ehdr->e_shoff);
349     shentsize = mcld::bswap16(ehdr->e_shentsize);
350     shnum     = mcld::bswap16(ehdr->e_shnum);
351     shstrtab  = mcld::bswap16(ehdr->e_shstrndx);
352   }
353 
354   // If the file has no section header table, e_shoff holds zero.
355   if (0x0 == shoff)
356     return true;
357 
358   llvm::ELF::Elf32_Shdr *shdr = NULL;
359   MemoryRegion* shdr_region = NULL;
360   uint32_t sh_name      = 0x0;
361   uint32_t sh_type      = 0x0;
362   uint32_t sh_flags     = 0x0;
363   uint32_t sh_offset    = 0x0;
364   uint32_t sh_size      = 0x0;
365   uint32_t sh_link      = 0x0;
366   uint32_t sh_info      = 0x0;
367   uint32_t sh_addralign = 0x0;
368 
369   // if shnum and shstrtab overflow, the actual values are in the 1st shdr
370   if (shnum == llvm::ELF::SHN_UNDEF || shstrtab == llvm::ELF::SHN_XINDEX) {
371     shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff,
372                                             shentsize);
373     shdr = reinterpret_cast<llvm::ELF::Elf32_Shdr*>(shdr_region->start());
374 
375     if (llvm::sys::IsLittleEndianHost) {
376       sh_size = shdr->sh_size;
377       sh_link = shdr->sh_link;
378     }
379     else {
380       sh_size = mcld::bswap32(shdr->sh_size);
381       sh_link = mcld::bswap32(shdr->sh_link);
382     }
383     pInput.memArea()->release(shdr_region);
384 
385     if (shnum == llvm::ELF::SHN_UNDEF)
386       shnum = sh_size;
387     if (shstrtab == llvm::ELF::SHN_XINDEX)
388       shstrtab = sh_link;
389 
390     shoff += shentsize;
391   }
392 
393   shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff,
394                                           shnum * shentsize);
395   llvm::ELF::Elf32_Shdr * shdrTab =
396     reinterpret_cast<llvm::ELF::Elf32_Shdr*>(shdr_region->start());
397 
398   // get .shstrtab first
399   shdr = &shdrTab[shstrtab];
400   if (llvm::sys::IsLittleEndianHost) {
401     sh_offset = shdr->sh_offset;
402     sh_size   = shdr->sh_size;
403   }
404   else {
405     sh_offset = mcld::bswap32(shdr->sh_offset);
406     sh_size   = mcld::bswap32(shdr->sh_size);
407   }
408 
409   MemoryRegion* sect_name_region = pInput.memArea()->request(
410                                       pInput.fileOffset() + sh_offset, sh_size);
411   const char* sect_name =
412                        reinterpret_cast<const char*>(sect_name_region->start());
413 
414   LinkInfoList link_info_list;
415 
416   // create all LDSections, including first NULL section.
417   for (size_t idx = 0; idx < shnum; ++idx) {
418     if (llvm::sys::IsLittleEndianHost) {
419       sh_name      = shdrTab[idx].sh_name;
420       sh_type      = shdrTab[idx].sh_type;
421       sh_flags     = shdrTab[idx].sh_flags;
422       sh_offset    = shdrTab[idx].sh_offset;
423       sh_size      = shdrTab[idx].sh_size;
424       sh_link      = shdrTab[idx].sh_link;
425       sh_info      = shdrTab[idx].sh_info;
426       sh_addralign = shdrTab[idx].sh_addralign;
427     }
428     else {
429       sh_name      = mcld::bswap32(shdrTab[idx].sh_name);
430       sh_type      = mcld::bswap32(shdrTab[idx].sh_type);
431       sh_flags     = mcld::bswap32(shdrTab[idx].sh_flags);
432       sh_offset    = mcld::bswap32(shdrTab[idx].sh_offset);
433       sh_size      = mcld::bswap32(shdrTab[idx].sh_size);
434       sh_link      = mcld::bswap32(shdrTab[idx].sh_link);
435       sh_info      = mcld::bswap32(shdrTab[idx].sh_info);
436       sh_addralign = mcld::bswap32(shdrTab[idx].sh_addralign);
437     }
438 
439     LDSection* section = IRBuilder::CreateELFHeader(pInput,
440                                                     sect_name+sh_name,
441                                                     sh_type,
442                                                     sh_flags,
443                                                     sh_addralign);
444     section->setSize(sh_size);
445     section->setOffset(sh_offset);
446     section->setInfo(sh_info);
447 
448     if (sh_link != 0x0 || sh_info != 0x0) {
449       LinkInfo link_info = { section, sh_link, sh_info };
450       link_info_list.push_back(link_info);
451     }
452   } // end of for
453 
454   // set up InfoLink
455   LinkInfoList::iterator info, infoEnd = link_info_list.end();
456   for (info = link_info_list.begin(); info != infoEnd; ++info) {
457     if (LDFileFormat::NamePool == info->section->kind() ||
458         LDFileFormat::Group == info->section->kind() ||
459         LDFileFormat::Note == info->section->kind()) {
460       info->section->setLink(pInput.context()->getSection(info->sh_link));
461       continue;
462     }
463     if (LDFileFormat::Relocation == info->section->kind()) {
464       info->section->setLink(pInput.context()->getSection(info->sh_info));
465       continue;
466     }
467   }
468 
469   pInput.memArea()->release(shdr_region);
470   pInput.memArea()->release(sect_name_region);
471 
472   return true;
473 }
474 
475 /// readSignature - read a symbol from the given Input and index in symtab
476 /// This is used to get the signature of a group section.
readSignature(Input & pInput,LDSection & pSymTab,uint32_t pSymIdx) const477 ResolveInfo* ELFReader<32, true>::readSignature(Input& pInput,
478                                                 LDSection& pSymTab,
479                                                 uint32_t pSymIdx) const
480 {
481   LDSection* symtab = &pSymTab;
482   LDSection* strtab = symtab->getLink();
483   assert(NULL != symtab && NULL != strtab);
484 
485   uint32_t offset = pInput.fileOffset() + symtab->offset() +
486                       sizeof(llvm::ELF::Elf32_Sym) * pSymIdx;
487   MemoryRegion* symbol_region =
488                 pInput.memArea()->request(offset, sizeof(llvm::ELF::Elf32_Sym));
489   llvm::ELF::Elf32_Sym* entry =
490                 reinterpret_cast<llvm::ELF::Elf32_Sym*>(symbol_region->start());
491 
492   uint32_t st_name  = 0x0;
493   uint8_t  st_info  = 0x0;
494   uint8_t  st_other = 0x0;
495   uint16_t st_shndx = 0x0;
496   st_info  = entry->st_info;
497   st_other = entry->st_other;
498   if (llvm::sys::IsLittleEndianHost) {
499     st_name  = entry->st_name;
500     st_shndx = entry->st_shndx;
501   }
502   else {
503     st_name  = mcld::bswap32(entry->st_name);
504     st_shndx = mcld::bswap16(entry->st_shndx);
505   }
506 
507   MemoryRegion* strtab_region = pInput.memArea()->request(
508                        pInput.fileOffset() + strtab->offset(), strtab->size());
509 
510   // get ld_name
511   llvm::StringRef ld_name(
512                     reinterpret_cast<char*>(strtab_region->start() + st_name));
513 
514   ResolveInfo* result = ResolveInfo::Create(ld_name);
515   result->setSource(pInput.type() == Input::DynObj);
516   result->setType(static_cast<ResolveInfo::Type>(st_info & 0xF));
517   result->setDesc(getSymDesc(st_shndx, pInput));
518   result->setBinding(getSymBinding((st_info >> 4), st_shndx, st_other));
519   result->setVisibility(getSymVisibility(st_other));
520 
521   // release regions
522   pInput.memArea()->release(symbol_region);
523   pInput.memArea()->release(strtab_region);
524 
525   return result;
526 }
527 
528 /// readDynamic - read ELF .dynamic in input dynobj
readDynamic(Input & pInput) const529 bool ELFReader<32, true>::readDynamic(Input& pInput) const
530 {
531   assert(pInput.type() == Input::DynObj);
532   const LDSection* dynamic_sect = pInput.context()->getSection(".dynamic");
533   if (NULL == dynamic_sect) {
534     fatal(diag::err_cannot_read_section) << ".dynamic";
535   }
536   const LDSection* dynstr_sect = dynamic_sect->getLink();
537   if (NULL == dynstr_sect) {
538     fatal(diag::err_cannot_read_section) << ".dynstr";
539   }
540 
541   MemoryRegion* dynamic_region = pInput.memArea()->request(
542            pInput.fileOffset() + dynamic_sect->offset(), dynamic_sect->size());
543 
544   MemoryRegion* dynstr_region = pInput.memArea()->request(
545              pInput.fileOffset() + dynstr_sect->offset(), dynstr_sect->size());
546 
547   assert(NULL != dynamic_region && NULL != dynstr_region);
548 
549   const llvm::ELF::Elf32_Dyn* dynamic =
550     (llvm::ELF::Elf32_Dyn*) dynamic_region->start();
551   const char* dynstr = (const char*) dynstr_region->start();
552   bool hasSOName = false;
553   size_t numOfEntries = dynamic_sect->size() / sizeof(llvm::ELF::Elf32_Dyn);
554 
555   for (size_t idx = 0; idx < numOfEntries; ++idx) {
556 
557     llvm::ELF::Elf32_Sword d_tag = 0x0;
558     llvm::ELF::Elf32_Word d_val = 0x0;
559 
560     if (llvm::sys::IsLittleEndianHost) {
561       d_tag = dynamic[idx].d_tag;
562       d_val = dynamic[idx].d_un.d_val;
563     } else {
564       d_tag = mcld::bswap32(dynamic[idx].d_tag);
565       d_val = mcld::bswap32(dynamic[idx].d_un.d_val);
566     }
567 
568     switch (d_tag) {
569       case llvm::ELF::DT_SONAME:
570         assert(d_val < dynstr_sect->size());
571         pInput.setName(sys::fs::Path(dynstr + d_val).filename().native());
572         hasSOName = true;
573         break;
574       case llvm::ELF::DT_NEEDED:
575         // TODO:
576         break;
577       case llvm::ELF::DT_NULL:
578       default:
579         break;
580     }
581   }
582 
583   // if there is no SONAME in .dynamic, then set it from input path
584   if (!hasSOName)
585     pInput.setName(pInput.path().filename().native());
586 
587   pInput.memArea()->release(dynamic_region);
588   pInput.memArea()->release(dynstr_region);
589   return true;
590 }
591 
592 //===----------------------------------------------------------------------===//
593 // ELFReader<64, true>
594 //===----------------------------------------------------------------------===//
595 /// constructor
ELFReader(GNULDBackend & pBackend)596 ELFReader<64, true>::ELFReader(GNULDBackend& pBackend)
597   : ELFReaderIF(pBackend) {
598 }
599 
600 /// destructor
~ELFReader()601 ELFReader<64, true>::~ELFReader()
602 {
603 }
604 
605 /// isELF - is this a ELF file
isELF(void * pELFHeader) const606 bool ELFReader<64, true>::isELF(void* pELFHeader) const
607 {
608   llvm::ELF::Elf64_Ehdr* hdr =
609                           reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader);
610   if (0 == memcmp(llvm::ELF::ElfMagic, hdr, 4))
611     return true;
612   return false;
613 }
614 
615 /// readRegularSection - read a regular section and create fragments.
616 bool
readRegularSection(Input & pInput,SectionData & pSD) const617 ELFReader<64, true>::readRegularSection(Input& pInput, SectionData& pSD) const
618 {
619   uint64_t offset = pInput.fileOffset() + pSD.getSection().offset();
620   uint64_t size = pSD.getSection().size();
621 
622   Fragment* frag = IRBuilder::CreateRegion(pInput, offset, size);
623   ObjectBuilder::AppendFragment(*frag, pSD);
624   return true;
625 }
626 
627 /// readSymbols - read ELF symbols and create LDSymbol
readSymbols(Input & pInput,IRBuilder & pBuilder,const MemoryRegion & pRegion,const char * pStrTab) const628 bool ELFReader<64, true>::readSymbols(Input& pInput,
629                                       IRBuilder& pBuilder,
630                                       const MemoryRegion& pRegion,
631                                       const char* pStrTab) const
632 {
633   // get number of symbols
634   size_t entsize = pRegion.size()/sizeof(llvm::ELF::Elf64_Sym);
635   const llvm::ELF::Elf64_Sym* symtab =
636                  reinterpret_cast<const llvm::ELF::Elf64_Sym*>(pRegion.start());
637 
638   uint32_t st_name  = 0x0;
639   uint64_t st_value = 0x0;
640   uint64_t st_size  = 0x0;
641   uint8_t  st_info  = 0x0;
642   uint8_t  st_other = 0x0;
643   uint16_t st_shndx = 0x0;
644 
645   // skip the first NULL symbol
646   pInput.context()->addSymbol(LDSymbol::Null());
647 
648   /// recording symbols added from DynObj to analyze weak alias
649   std::vector<AliasInfo> potential_aliases;
650   bool is_dyn_obj = (pInput.type()==Input::DynObj);
651   for (size_t idx = 1; idx < entsize; ++idx) {
652     st_info  = symtab[idx].st_info;
653     st_other = symtab[idx].st_other;
654 
655     if (llvm::sys::IsLittleEndianHost) {
656       st_name  = symtab[idx].st_name;
657       st_value = symtab[idx].st_value;
658       st_size  = symtab[idx].st_size;
659       st_shndx = symtab[idx].st_shndx;
660     }
661     else {
662       st_name  = mcld::bswap32(symtab[idx].st_name);
663       st_value = mcld::bswap64(symtab[idx].st_value);
664       st_size  = mcld::bswap64(symtab[idx].st_size);
665       st_shndx = mcld::bswap16(symtab[idx].st_shndx);
666     }
667 
668     // If the section should not be included, set the st_shndx SHN_UNDEF
669     // - A section in interrelated groups are not included.
670     if (pInput.type() == Input::Object &&
671         st_shndx < llvm::ELF::SHN_LORESERVE &&
672         st_shndx != llvm::ELF::SHN_UNDEF) {
673       if (NULL == pInput.context()->getSection(st_shndx))
674         st_shndx = llvm::ELF::SHN_UNDEF;
675     }
676 
677     // get ld_type
678     ResolveInfo::Type ld_type = getSymType(st_info, st_shndx);
679 
680     // get ld_desc
681     ResolveInfo::Desc ld_desc = getSymDesc(st_shndx, pInput);
682 
683     // get ld_binding
684     ResolveInfo::Binding ld_binding = getSymBinding((st_info >> 4), st_shndx, st_other);
685 
686     // get ld_value - ld_value must be section relative.
687     uint64_t ld_value = getSymValue(st_value, st_shndx, pInput);
688 
689     // get ld_vis
690     ResolveInfo::Visibility ld_vis = getSymVisibility(st_other);
691 
692     // get section
693     LDSection* section = NULL;
694     if (st_shndx < llvm::ELF::SHN_LORESERVE) // including ABS and COMMON
695       section = pInput.context()->getSection(st_shndx);
696 
697     // get ld_name
698     std::string ld_name;
699     if (ResolveInfo::Section == ld_type) {
700       // Section symbol's st_name is the section index.
701       assert(NULL != section && "get a invalid section");
702       ld_name = section->name();
703     }
704     else {
705       ld_name = std::string(pStrTab + st_name);
706     }
707 
708     LDSymbol *psym =
709             pBuilder.AddSymbol(pInput,
710                                ld_name,
711                                ld_type,
712                                ld_desc,
713                                ld_binding,
714                                st_size,
715                                ld_value,
716                                section, ld_vis);
717 
718     if ( is_dyn_obj
719          && NULL!=psym
720          && ResolveInfo::Undefined!=ld_desc
721          && (ResolveInfo::Global==ld_binding ||
722              ResolveInfo::Weak==ld_binding)
723          && ResolveInfo::Object==ld_type ) {
724       AliasInfo p;
725       p.pt_alias = psym;
726       p.ld_binding = ld_binding;
727       p.ld_value = ld_value;
728       potential_aliases.push_back(p);
729     }
730 
731   } // end of for loop
732 
733   // analyze weak alias here
734   if ( is_dyn_obj ) {
735     // sort symbols by symbol value and then weak before strong
736     std::sort( potential_aliases.begin(), potential_aliases.end(), less);
737 
738     // for each weak symbol, find out all its aliases, and
739     // then link them as a circular list in Module
740     std::vector<AliasInfo>::iterator sym_it, sym_e;
741     sym_e = potential_aliases.end();
742     for (sym_it = potential_aliases.begin(); sym_it!=sym_e; ++sym_it) {
743       if (ResolveInfo::Weak!=sym_it->ld_binding)
744         continue;
745 
746       Module& pModule = pBuilder.getModule();
747       std::vector<AliasInfo>::iterator alias_it = sym_it+1;
748       while(alias_it!=sym_e) {
749         if ( sym_it->ld_value != alias_it->ld_value )
750           break;
751 
752         if (sym_it+1==alias_it)
753           pModule.CreateAliasList(*sym_it->pt_alias->resolveInfo());
754         pModule.addAlias(*alias_it->pt_alias->resolveInfo());
755         ++alias_it;
756       }
757 
758       sym_it = alias_it-1;
759     }// end of for loop
760   }
761   return true;
762 }
763 
764 //===----------------------------------------------------------------------===//
765 // ELFReader::read relocations - read ELF rela and rel, and create Relocation
766 //===----------------------------------------------------------------------===//
767 /// ELFReader::readRela - read ELF rela and create Relocation
readRela(Input & pInput,LDSection & pSection,const MemoryRegion & pRegion) const768 bool ELFReader<64, true>::readRela(Input& pInput,
769                                    LDSection& pSection,
770                                    const MemoryRegion& pRegion) const
771 {
772   // get the number of rela
773   size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf64_Rela);
774   const llvm::ELF::Elf64_Rela* relaTab =
775                 reinterpret_cast<const llvm::ELF::Elf64_Rela*>(pRegion.start());
776 
777   for (size_t idx=0; idx < entsize; ++idx) {
778     uint64_t r_offset = 0x0;
779     uint64_t r_info   = 0x0;
780     int64_t  r_addend = 0;
781     if (llvm::sys::IsLittleEndianHost) {
782       r_offset = relaTab[idx].r_offset;
783       r_info   = relaTab[idx].r_info;
784       r_addend = relaTab[idx].r_addend;
785     }
786     else {
787       r_offset = mcld::bswap64(relaTab[idx].r_offset);
788       r_info   = mcld::bswap64(relaTab[idx].r_info);
789       r_addend = mcld::bswap64(relaTab[idx].r_addend);
790     }
791 
792     uint32_t  r_type = static_cast<uint32_t>(r_info);
793     uint32_t r_sym  = (r_info >> 32);
794     LDSymbol* symbol = pInput.context()->getSymbol(r_sym);
795     if (NULL == symbol) {
796       fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path();
797     }
798 
799     IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset, r_addend);
800   } // end of for
801   return true;
802 }
803 
804 /// readRel - read ELF rel and create Relocation
readRel(Input & pInput,LDSection & pSection,const MemoryRegion & pRegion) const805 bool ELFReader<64, true>::readRel(Input& pInput,
806                                   LDSection& pSection,
807                                   const MemoryRegion& pRegion) const
808 {
809   // get the number of rel
810   size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf64_Rel);
811   const llvm::ELF::Elf64_Rel* relTab =
812                  reinterpret_cast<const llvm::ELF::Elf64_Rel*>(pRegion.start());
813 
814   for (size_t idx=0; idx < entsize; ++idx) {
815     uint64_t r_offset = 0x0;
816     uint64_t r_info   = 0x0;
817     if (llvm::sys::IsLittleEndianHost) {
818       r_offset = relTab[idx].r_offset;
819       r_info   = relTab[idx].r_info;
820     }
821     else {
822       r_offset = mcld::bswap64(relTab[idx].r_offset);
823       r_info   = mcld::bswap64(relTab[idx].r_info);
824     }
825 
826     uint32_t  r_type = static_cast<uint32_t>(r_info);
827     uint32_t r_sym  = (r_info >> 32);
828 
829     LDSymbol* symbol = pInput.context()->getSymbol(r_sym);
830     if (NULL == symbol) {
831       fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path();
832     }
833 
834     IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset);
835   } // end of for
836   return true;
837 }
838 
839 /// isMyEndian - is this ELF file in the same endian to me?
isMyEndian(void * pELFHeader) const840 bool ELFReader<64, true>::isMyEndian(void* pELFHeader) const
841 {
842   llvm::ELF::Elf64_Ehdr* hdr =
843                           reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader);
844 
845   return (hdr->e_ident[llvm::ELF::EI_DATA] == llvm::ELF::ELFDATA2LSB);
846 }
847 
848 /// isMyMachine - is this ELF file generated for the same machine.
isMyMachine(void * pELFHeader) const849 bool ELFReader<64, true>::isMyMachine(void* pELFHeader) const
850 {
851   llvm::ELF::Elf64_Ehdr* hdr =
852                           reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader);
853 
854   if (llvm::sys::IsLittleEndianHost)
855     return (hdr->e_machine == target().getInfo().machine());
856   return (mcld::bswap16(hdr->e_machine) == target().getInfo().machine());
857 }
858 
859 /// fileType - return the file type
fileType(void * pELFHeader) const860 Input::Type ELFReader<64, true>::fileType(void* pELFHeader) const
861 {
862   llvm::ELF::Elf64_Ehdr* hdr =
863                           reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader);
864   uint32_t type = 0x0;
865   if (llvm::sys::IsLittleEndianHost)
866     type = hdr->e_type;
867   else
868     type = mcld::bswap16(hdr->e_type);
869 
870   switch(type) {
871   case llvm::ELF::ET_REL:
872     return Input::Object;
873   case llvm::ELF::ET_EXEC:
874     return Input::Exec;
875   case llvm::ELF::ET_DYN:
876     return Input::DynObj;
877   case llvm::ELF::ET_CORE:
878     return Input::CoreFile;
879   case llvm::ELF::ET_NONE:
880   default:
881     return Input::Unknown;
882   }
883 }
884 
885 /// readSectionHeaders - read ELF section header table and create LDSections
886 bool
readSectionHeaders(Input & pInput,void * pELFHeader) const887 ELFReader<64, true>::readSectionHeaders(Input& pInput, void* pELFHeader) const
888 {
889   llvm::ELF::Elf64_Ehdr* ehdr =
890                           reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader);
891 
892   uint64_t shoff     = 0x0;
893   uint16_t shentsize = 0x0;
894   uint32_t shnum     = 0x0;
895   uint32_t shstrtab  = 0x0;
896 
897   if (llvm::sys::IsLittleEndianHost) {
898     shoff     = ehdr->e_shoff;
899     shentsize = ehdr->e_shentsize;
900     shnum     = ehdr->e_shnum;
901     shstrtab  = ehdr->e_shstrndx;
902   }
903   else {
904     shoff     = mcld::bswap64(ehdr->e_shoff);
905     shentsize = mcld::bswap16(ehdr->e_shentsize);
906     shnum     = mcld::bswap16(ehdr->e_shnum);
907     shstrtab  = mcld::bswap16(ehdr->e_shstrndx);
908   }
909 
910   // If the file has no section header table, e_shoff holds zero.
911   if (0x0 == shoff)
912     return true;
913 
914   llvm::ELF::Elf64_Shdr *shdr = NULL;
915   MemoryRegion* shdr_region = NULL;
916   uint32_t sh_name      = 0x0;
917   uint32_t sh_type      = 0x0;
918   uint64_t sh_flags     = 0x0;
919   uint64_t sh_offset    = 0x0;
920   uint64_t sh_size      = 0x0;
921   uint32_t sh_link      = 0x0;
922   uint32_t sh_info      = 0x0;
923   uint64_t sh_addralign = 0x0;
924 
925   // if shnum and shstrtab overflow, the actual values are in the 1st shdr
926   if (shnum == llvm::ELF::SHN_UNDEF || shstrtab == llvm::ELF::SHN_XINDEX) {
927     shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff,
928                                             shentsize);
929     shdr = reinterpret_cast<llvm::ELF::Elf64_Shdr*>(shdr_region->start());
930 
931     if (llvm::sys::IsLittleEndianHost) {
932       sh_size = shdr->sh_size;
933       sh_link = shdr->sh_link;
934     }
935     else {
936       sh_size = mcld::bswap64(shdr->sh_size);
937       sh_link = mcld::bswap32(shdr->sh_link);
938     }
939     pInput.memArea()->release(shdr_region);
940 
941     if (shnum == llvm::ELF::SHN_UNDEF)
942       shnum = sh_size;
943     if (shstrtab == llvm::ELF::SHN_XINDEX)
944       shstrtab = sh_link;
945 
946     shoff += shentsize;
947   }
948 
949   shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff,
950                                           shnum * shentsize);
951   llvm::ELF::Elf64_Shdr * shdrTab =
952     reinterpret_cast<llvm::ELF::Elf64_Shdr*>(shdr_region->start());
953 
954   // get .shstrtab first
955   shdr = &shdrTab[shstrtab];
956   if (llvm::sys::IsLittleEndianHost) {
957     sh_offset = shdr->sh_offset;
958     sh_size   = shdr->sh_size;
959   }
960   else {
961     sh_offset = mcld::bswap64(shdr->sh_offset);
962     sh_size   = mcld::bswap64(shdr->sh_size);
963   }
964 
965   MemoryRegion* sect_name_region = pInput.memArea()->request(
966                                       pInput.fileOffset() + sh_offset, sh_size);
967   const char* sect_name =
968                        reinterpret_cast<const char*>(sect_name_region->start());
969 
970   LinkInfoList link_info_list;
971 
972   // create all LDSections, including first NULL section.
973   for (size_t idx = 0; idx < shnum; ++idx) {
974     if (llvm::sys::IsLittleEndianHost) {
975       sh_name      = shdrTab[idx].sh_name;
976       sh_type      = shdrTab[idx].sh_type;
977       sh_flags     = shdrTab[idx].sh_flags;
978       sh_offset    = shdrTab[idx].sh_offset;
979       sh_size      = shdrTab[idx].sh_size;
980       sh_link      = shdrTab[idx].sh_link;
981       sh_info      = shdrTab[idx].sh_info;
982       sh_addralign = shdrTab[idx].sh_addralign;
983     }
984     else {
985       sh_name      = mcld::bswap32(shdrTab[idx].sh_name);
986       sh_type      = mcld::bswap32(shdrTab[idx].sh_type);
987       sh_flags     = mcld::bswap64(shdrTab[idx].sh_flags);
988       sh_offset    = mcld::bswap64(shdrTab[idx].sh_offset);
989       sh_size      = mcld::bswap64(shdrTab[idx].sh_size);
990       sh_link      = mcld::bswap32(shdrTab[idx].sh_link);
991       sh_info      = mcld::bswap32(shdrTab[idx].sh_info);
992       sh_addralign = mcld::bswap64(shdrTab[idx].sh_addralign);
993     }
994 
995     LDSection* section = IRBuilder::CreateELFHeader(pInput,
996                                                     sect_name+sh_name,
997                                                     sh_type,
998                                                     sh_flags,
999                                                     sh_addralign);
1000     section->setSize(sh_size);
1001     section->setOffset(sh_offset);
1002     section->setInfo(sh_info);
1003 
1004     if (sh_link != 0x0 || sh_info != 0x0) {
1005       LinkInfo link_info = { section, sh_link, sh_info };
1006       link_info_list.push_back(link_info);
1007     }
1008   } // end of for
1009 
1010   // set up InfoLink
1011   LinkInfoList::iterator info, infoEnd = link_info_list.end();
1012   for (info = link_info_list.begin(); info != infoEnd; ++info) {
1013     if (LDFileFormat::NamePool == info->section->kind() ||
1014         LDFileFormat::Group == info->section->kind() ||
1015         LDFileFormat::Note == info->section->kind()) {
1016       info->section->setLink(pInput.context()->getSection(info->sh_link));
1017       continue;
1018     }
1019     if (LDFileFormat::Relocation == info->section->kind()) {
1020       info->section->setLink(pInput.context()->getSection(info->sh_info));
1021       continue;
1022     }
1023   }
1024 
1025   pInput.memArea()->release(shdr_region);
1026   pInput.memArea()->release(sect_name_region);
1027 
1028   return true;
1029 }
1030 
1031 /// readSignature - read a symbol from the given Input and index in symtab
1032 /// This is used to get the signature of a group section.
readSignature(Input & pInput,LDSection & pSymTab,uint32_t pSymIdx) const1033 ResolveInfo* ELFReader<64, true>::readSignature(Input& pInput,
1034                                                 LDSection& pSymTab,
1035                                                 uint32_t pSymIdx) const
1036 {
1037   LDSection* symtab = &pSymTab;
1038   LDSection* strtab = symtab->getLink();
1039   assert(NULL != symtab && NULL != strtab);
1040 
1041   uint64_t offset = pInput.fileOffset() + symtab->offset() +
1042                       sizeof(llvm::ELF::Elf64_Sym) * pSymIdx;
1043   MemoryRegion* symbol_region =
1044                 pInput.memArea()->request(offset, sizeof(llvm::ELF::Elf64_Sym));
1045   llvm::ELF::Elf64_Sym* entry =
1046                 reinterpret_cast<llvm::ELF::Elf64_Sym*>(symbol_region->start());
1047 
1048   uint32_t st_name  = 0x0;
1049   uint8_t  st_info  = 0x0;
1050   uint8_t  st_other = 0x0;
1051   uint16_t st_shndx = 0x0;
1052   st_info  = entry->st_info;
1053   st_other = entry->st_other;
1054   if (llvm::sys::IsLittleEndianHost) {
1055     st_name  = entry->st_name;
1056     st_shndx = entry->st_shndx;
1057   }
1058   else {
1059     st_name  = mcld::bswap32(entry->st_name);
1060     st_shndx = mcld::bswap16(entry->st_shndx);
1061   }
1062 
1063   MemoryRegion* strtab_region = pInput.memArea()->request(
1064                        pInput.fileOffset() + strtab->offset(), strtab->size());
1065 
1066   // get ld_name
1067   llvm::StringRef ld_name(
1068                     reinterpret_cast<char*>(strtab_region->start() + st_name));
1069 
1070   ResolveInfo* result = ResolveInfo::Create(ld_name);
1071   result->setSource(pInput.type() == Input::DynObj);
1072   result->setType(static_cast<ResolveInfo::Type>(st_info & 0xF));
1073   result->setDesc(getSymDesc(st_shndx, pInput));
1074   result->setBinding(getSymBinding((st_info >> 4), st_shndx, st_other));
1075   result->setVisibility(getSymVisibility(st_other));
1076 
1077   // release regions
1078   pInput.memArea()->release(symbol_region);
1079   pInput.memArea()->release(strtab_region);
1080 
1081   return result;
1082 }
1083 
1084 /// readDynamic - read ELF .dynamic in input dynobj
readDynamic(Input & pInput) const1085 bool ELFReader<64, true>::readDynamic(Input& pInput) const
1086 {
1087   assert(pInput.type() == Input::DynObj);
1088   const LDSection* dynamic_sect = pInput.context()->getSection(".dynamic");
1089   if (NULL == dynamic_sect) {
1090     fatal(diag::err_cannot_read_section) << ".dynamic";
1091   }
1092   const LDSection* dynstr_sect = dynamic_sect->getLink();
1093   if (NULL == dynstr_sect) {
1094     fatal(diag::err_cannot_read_section) << ".dynstr";
1095   }
1096 
1097   MemoryRegion* dynamic_region = pInput.memArea()->request(
1098            pInput.fileOffset() + dynamic_sect->offset(), dynamic_sect->size());
1099 
1100   MemoryRegion* dynstr_region = pInput.memArea()->request(
1101              pInput.fileOffset() + dynstr_sect->offset(), dynstr_sect->size());
1102 
1103   assert(NULL != dynamic_region && NULL != dynstr_region);
1104 
1105   const llvm::ELF::Elf64_Dyn* dynamic =
1106     (llvm::ELF::Elf64_Dyn*) dynamic_region->start();
1107   const char* dynstr = (const char*) dynstr_region->start();
1108   bool hasSOName = false;
1109   size_t numOfEntries = dynamic_sect->size() / sizeof(llvm::ELF::Elf64_Dyn);
1110 
1111   for (size_t idx = 0; idx < numOfEntries; ++idx) {
1112 
1113     llvm::ELF::Elf64_Sxword d_tag = 0x0;
1114     llvm::ELF::Elf64_Xword d_val = 0x0;
1115 
1116     if (llvm::sys::IsLittleEndianHost) {
1117       d_tag = dynamic[idx].d_tag;
1118       d_val = dynamic[idx].d_un.d_val;
1119     } else {
1120       d_tag = mcld::bswap64(dynamic[idx].d_tag);
1121       d_val = mcld::bswap64(dynamic[idx].d_un.d_val);
1122     }
1123 
1124     switch (d_tag) {
1125       case llvm::ELF::DT_SONAME:
1126         assert(d_val < dynstr_sect->size());
1127         pInput.setName(sys::fs::Path(dynstr + d_val).filename().native());
1128         hasSOName = true;
1129         break;
1130       case llvm::ELF::DT_NEEDED:
1131         // TODO:
1132         break;
1133       case llvm::ELF::DT_NULL:
1134       default:
1135         break;
1136     }
1137   }
1138 
1139   // if there is no SONAME in .dynamic, then set it from input path
1140   if (!hasSOName)
1141     pInput.setName(pInput.path().filename().native());
1142 
1143   pInput.memArea()->release(dynamic_region);
1144   pInput.memArea()->release(dynstr_region);
1145   return true;
1146 }
1147 
1148