• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ELFObjectReader.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/ELFObjectReader.h>
10 
11 #include <string>
12 #include <cassert>
13 
14 #include <llvm/Support/ELF.h>
15 #include <llvm/ADT/Twine.h>
16 
17 #include <mcld/IRBuilder.h>
18 #include <mcld/MC/MCLDInput.h>
19 #include <mcld/LD/ELFReader.h>
20 #include <mcld/LD/EhFrameReader.h>
21 #include <mcld/LD/EhFrame.h>
22 #include <mcld/Target/GNULDBackend.h>
23 #include <mcld/Support/MsgHandling.h>
24 #include <mcld/Object/ObjectBuilder.h>
25 
26 using namespace mcld;
27 
28 //===----------------------------------------------------------------------===//
29 // ELFObjectReader
30 //===----------------------------------------------------------------------===//
31 /// constructor
ELFObjectReader(GNULDBackend & pBackend,IRBuilder & pBuilder,const LinkerConfig & pConfig)32 ELFObjectReader::ELFObjectReader(GNULDBackend& pBackend,
33                                  IRBuilder& pBuilder,
34                                  const LinkerConfig& pConfig)
35   : ObjectReader(),
36     m_pELFReader(NULL),
37     m_pEhFrameReader(NULL),
38     m_Builder(pBuilder),
39     m_ReadFlag(ParseEhFrame),
40     m_Backend(pBackend),
41     m_Config(pConfig) {
42   if (pConfig.targets().is32Bits() && pConfig.targets().isLittleEndian()) {
43     m_pELFReader = new ELFReader<32, true>(pBackend);
44   }
45   else if (pConfig.targets().is64Bits() && pConfig.targets().isLittleEndian()) {
46     m_pELFReader = new ELFReader<64, true>(pBackend);
47   }
48 
49   m_pEhFrameReader = new EhFrameReader();
50 }
51 
52 /// destructor
~ELFObjectReader()53 ELFObjectReader::~ELFObjectReader()
54 {
55   delete m_pELFReader;
56   delete m_pEhFrameReader;
57 }
58 
59 /// isMyFormat
isMyFormat(Input & pInput) const60 bool ELFObjectReader::isMyFormat(Input &pInput) const
61 {
62   assert(pInput.hasMemArea());
63 
64   // Don't warning about the frequently requests.
65   // MemoryArea has a list of cache to handle this.
66   size_t hdr_size = m_pELFReader->getELFHeaderSize();
67   MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(),
68                                                      hdr_size);
69 
70   uint8_t* ELF_hdr = region->start();
71   bool result = true;
72   if (!m_pELFReader->isELF(ELF_hdr))
73     result = false;
74   else if (!m_pELFReader->isMyEndian(ELF_hdr))
75     result = false;
76   else if (!m_pELFReader->isMyMachine(ELF_hdr))
77     result = false;
78   else if (Input::Object != m_pELFReader->fileType(ELF_hdr))
79     result = false;
80   pInput.memArea()->release(region);
81   return result;
82 }
83 
84 /// readHeader - read section header and create LDSections.
readHeader(Input & pInput)85 bool ELFObjectReader::readHeader(Input& pInput)
86 {
87   assert(pInput.hasMemArea());
88 
89   size_t hdr_size = m_pELFReader->getELFHeaderSize();
90   MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(),
91                                                      hdr_size);
92   uint8_t* ELF_hdr = region->start();
93   bool result = m_pELFReader->readSectionHeaders(pInput, ELF_hdr);
94   pInput.memArea()->release(region);
95   return result;
96 }
97 
98 /// readSections - read all regular sections.
readSections(Input & pInput)99 bool ELFObjectReader::readSections(Input& pInput)
100 {
101   // handle sections
102   LDContext::sect_iterator section, sectEnd = pInput.context()->sectEnd();
103   for (section = pInput.context()->sectBegin(); section != sectEnd; ++section) {
104     // ignore the section if the LDSection* in input context is NULL
105     if (NULL == *section)
106         continue;
107 
108     switch((*section)->kind()) {
109       /** group sections **/
110       case LDFileFormat::Group: {
111         assert(NULL != (*section)->getLink());
112         ResolveInfo* signature =
113               m_pELFReader->readSignature(pInput,
114                                           *(*section)->getLink(),
115                                           (*section)->getInfo());
116 
117         bool exist = false;
118         if (0 == signature->nameSize() &&
119             ResolveInfo::Section == signature->type()) {
120           // if the signature is a section symbol in input object, we use the
121           // section name as group signature.
122           signatures().insert((*section)->name(), exist);
123         } else {
124           signatures().insert(signature->name(), exist);
125         }
126 
127         if (exist) {
128           // if this is not the first time we see this group signature, then
129           // ignore all the members in this group (set Ignore)
130           MemoryRegion* region = pInput.memArea()->request(
131                pInput.fileOffset() + (*section)->offset(), (*section)->size());
132           llvm::ELF::Elf32_Word* value =
133                      reinterpret_cast<llvm::ELF::Elf32_Word*>(region->start());
134 
135           size_t size = region->size() / sizeof(llvm::ELF::Elf32_Word);
136           if (llvm::ELF::GRP_COMDAT == *value) {
137             for (size_t index = 1; index < size; ++index) {
138               pInput.context()->getSection(value[index])->setKind(LDFileFormat::Ignore);
139             }
140           }
141           pInput.memArea()->release(region);
142         }
143         ResolveInfo::Destroy(signature);
144         break;
145       }
146       /** linkonce sections **/
147       case LDFileFormat::LinkOnce: {
148         bool exist = false;
149         // .gnu.linkonce + "." + type + "." + name
150         llvm::StringRef name(llvm::StringRef((*section)->name()).drop_front(14));
151         signatures().insert(name.split(".").second, exist);
152         if (!exist) {
153           if (name.startswith("wi")) {
154             (*section)->setKind(LDFileFormat::Debug);
155             if (m_Config.options().stripDebug())
156               (*section)->setKind(LDFileFormat::Ignore);
157             else {
158               SectionData* sd = IRBuilder::CreateSectionData(**section);
159               if (!m_pELFReader->readRegularSection(pInput, *sd))
160                 fatal(diag::err_cannot_read_section) << (*section)->name();
161             }
162           } else {
163             (*section)->setKind(LDFileFormat::Regular);
164             SectionData* sd = IRBuilder::CreateSectionData(**section);
165             if (!m_pELFReader->readRegularSection(pInput, *sd))
166               fatal(diag::err_cannot_read_section) << (*section)->name();
167           }
168         } else {
169           (*section)->setKind(LDFileFormat::Ignore);
170         }
171         break;
172       }
173       /** relocation sections **/
174       case LDFileFormat::Relocation: {
175         assert(NULL != (*section)->getLink());
176         size_t link_index = (*section)->getLink()->index();
177         LDSection* link_sect = pInput.context()->getSection(link_index);
178         if (NULL == link_sect || LDFileFormat::Ignore == link_sect->kind()) {
179           // Relocation sections of group members should also be part of the
180           // group. Thus, if the associated member sections are ignored, the
181           // related relocations should be also ignored.
182           (*section)->setKind(LDFileFormat::Ignore);
183         }
184         break;
185       }
186       /** normal sections **/
187       // FIXME: support Version Kind
188       case LDFileFormat::Version:
189       // FIXME: support GCCExceptTable Kind
190       case LDFileFormat::GCCExceptTable:
191       /** Fall through **/
192       case LDFileFormat::Regular:
193       case LDFileFormat::Note:
194       case LDFileFormat::MetaData: {
195         SectionData* sd = IRBuilder::CreateSectionData(**section);
196         if (!m_pELFReader->readRegularSection(pInput, *sd))
197           fatal(diag::err_cannot_read_section) << (*section)->name();
198         break;
199       }
200       case LDFileFormat::Debug: {
201         if (m_Config.options().stripDebug()) {
202           (*section)->setKind(LDFileFormat::Ignore);
203         }
204         else {
205           SectionData* sd = IRBuilder::CreateSectionData(**section);
206           if (!m_pELFReader->readRegularSection(pInput, *sd)) {
207             fatal(diag::err_cannot_read_section) << (*section)->name();
208           }
209         }
210         break;
211       }
212       case LDFileFormat::EhFrame: {
213         EhFrame* eh_frame = IRBuilder::CreateEhFrame(**section);
214 
215         if (m_Config.options().hasEhFrameHdr() &&
216             (m_ReadFlag & ParseEhFrame)) {
217 
218           // if --eh-frame-hdr option is given, parse .eh_frame.
219           if (!m_pEhFrameReader->read<32, true>(pInput, *eh_frame)) {
220             // if we failed to parse a .eh_frame, we should not parse the rest
221             // .eh_frame.
222             m_ReadFlag ^= ParseEhFrame;
223           }
224         }
225         else {
226           if (!m_pELFReader->readRegularSection(pInput,
227                                                 *eh_frame->getSectionData())) {
228             fatal(diag::err_cannot_read_section) << (*section)->name();
229           }
230         }
231         break;
232       }
233       /** target dependent sections **/
234       case LDFileFormat::Target: {
235         SectionData* sd = IRBuilder::CreateSectionData(**section);
236         if (!m_Backend.readSection(pInput, *sd)) {
237           fatal(diag::err_cannot_read_target_section) << (*section)->name();
238         }
239         break;
240       }
241       /** BSS sections **/
242       case LDFileFormat::BSS: {
243         IRBuilder::CreateBSS(**section);
244         break;
245       }
246       // ignore
247       case LDFileFormat::Null:
248       case LDFileFormat::NamePool:
249       case LDFileFormat::Ignore:
250       case LDFileFormat::StackNote:
251         continue;
252       // warning
253       case LDFileFormat::EhFrameHdr:
254       default: {
255         warning(diag::warn_illegal_input_section) << (*section)->name()
256                                                   << pInput.name()
257                                                   << pInput.path();
258         break;
259       }
260     }
261   } // end of for all sections
262 
263   return true;
264 }
265 
266 /// readSymbols - read symbols from the input relocatable object.
readSymbols(Input & pInput)267 bool ELFObjectReader::readSymbols(Input& pInput)
268 {
269   assert(pInput.hasMemArea());
270 
271   LDSection* symtab_shdr = pInput.context()->getSection(".symtab");
272   if (NULL == symtab_shdr) {
273     note(diag::note_has_no_symtab) << pInput.name()
274                                    << pInput.path()
275                                    << ".symtab";
276     return true;
277   }
278 
279   LDSection* strtab_shdr = symtab_shdr->getLink();
280   if (NULL == strtab_shdr) {
281     fatal(diag::fatal_cannot_read_strtab) << pInput.name()
282                                           << pInput.path()
283                                           << ".symtab";
284     return false;
285   }
286 
287   MemoryRegion* symtab_region = pInput.memArea()->request(
288              pInput.fileOffset() + symtab_shdr->offset(), symtab_shdr->size());
289   MemoryRegion* strtab_region = pInput.memArea()->request(
290              pInput.fileOffset() + strtab_shdr->offset(), strtab_shdr->size());
291   char* strtab = reinterpret_cast<char*>(strtab_region->start());
292   bool result = m_pELFReader->readSymbols(pInput,
293                                           m_Builder,
294                                           *symtab_region,
295                                           strtab);
296   pInput.memArea()->release(symtab_region);
297   pInput.memArea()->release(strtab_region);
298   return result;
299 }
300 
readRelocations(Input & pInput)301 bool ELFObjectReader::readRelocations(Input& pInput)
302 {
303   assert(pInput.hasMemArea());
304 
305   MemoryArea* mem = pInput.memArea();
306   LDContext::sect_iterator rs, rsEnd = pInput.context()->relocSectEnd();
307   for (rs = pInput.context()->relocSectBegin(); rs != rsEnd; ++rs) {
308     if (LDFileFormat::Ignore == (*rs)->kind())
309       continue;
310 
311     uint32_t offset = pInput.fileOffset() + (*rs)->offset();
312     uint32_t size = (*rs)->size();
313     MemoryRegion* region = mem->request(offset, size);
314     IRBuilder::CreateRelocData(**rs); ///< create relocation data for the header
315     switch ((*rs)->type()) {
316       case llvm::ELF::SHT_RELA: {
317         if (!m_pELFReader->readRela(pInput, **rs, *region)) {
318           mem->release(region);
319           return false;
320         }
321         break;
322       }
323       case llvm::ELF::SHT_REL: {
324         if (!m_pELFReader->readRel(pInput, **rs, *region)) {
325           mem->release(region);
326           return false;
327         }
328         break;
329       }
330       default: { ///< should not enter
331         mem->release(region);
332         return false;
333       }
334     } // end of switch
335 
336     mem->release(region);
337   } // end of for all relocation data
338 
339   return true;
340 }
341 
342