• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ELFDynObjReader.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/ELFDynObjReader.h"
10 
11 #include "mcld/IRBuilder.h"
12 #include "mcld/LinkerConfig.h"
13 #include "mcld/LD/ELFReader.h"
14 #include "mcld/LD/LDContext.h"
15 #include "mcld/MC/Input.h"
16 #include "mcld/Support/MemoryArea.h"
17 #include "mcld/Target/GNULDBackend.h"
18 
19 #include <llvm/ADT/StringRef.h>
20 #include <llvm/ADT/Twine.h>
21 #include <llvm/Support/ErrorHandling.h>
22 
23 #include <string>
24 
25 namespace mcld {
26 
27 //===----------------------------------------------------------------------===//
28 // ELFDynObjReader
29 //===----------------------------------------------------------------------===//
ELFDynObjReader(GNULDBackend & pBackend,IRBuilder & pBuilder,const LinkerConfig & pConfig)30 ELFDynObjReader::ELFDynObjReader(GNULDBackend& pBackend,
31                                  IRBuilder& pBuilder,
32                                  const LinkerConfig& pConfig)
33     : DynObjReader(), m_pELFReader(0), m_Builder(pBuilder) {
34   if (pConfig.targets().is32Bits() && pConfig.targets().isLittleEndian())
35     m_pELFReader = new ELFReader<32, true>(pBackend);
36   else if (pConfig.targets().is64Bits() && pConfig.targets().isLittleEndian())
37     m_pELFReader = new ELFReader<64, true>(pBackend);
38 }
39 
~ELFDynObjReader()40 ELFDynObjReader::~ELFDynObjReader() {
41   delete m_pELFReader;
42 }
43 
44 /// isMyFormat
isMyFormat(Input & pInput,bool & pContinue) const45 bool ELFDynObjReader::isMyFormat(Input& pInput, bool& pContinue) const {
46   assert(pInput.hasMemArea());
47 
48   // Don't warning about the frequently requests.
49   // MemoryArea has a list of cache to handle this.
50   size_t hdr_size = m_pELFReader->getELFHeaderSize();
51   if (pInput.memArea()->size() < hdr_size)
52     return false;
53 
54   llvm::StringRef region =
55       pInput.memArea()->request(pInput.fileOffset(), hdr_size);
56 
57   const char* ELF_hdr = region.begin();
58   bool result = true;
59   if (!m_pELFReader->isELF(ELF_hdr)) {
60     pContinue = true;
61     result = false;
62   } else if (Input::DynObj != m_pELFReader->fileType(ELF_hdr)) {
63     pContinue = true;
64     result = false;
65   } else if (!m_pELFReader->isMyEndian(ELF_hdr)) {
66     pContinue = false;
67     result = false;
68   } else if (!m_pELFReader->isMyMachine(ELF_hdr)) {
69     pContinue = false;
70     result = false;
71   }
72   return result;
73 }
74 
75 /// readHeader
readHeader(Input & pInput)76 bool ELFDynObjReader::readHeader(Input& pInput) {
77   assert(pInput.hasMemArea());
78 
79   size_t hdr_size = m_pELFReader->getELFHeaderSize();
80   llvm::StringRef region =
81       pInput.memArea()->request(pInput.fileOffset(), hdr_size);
82   const char* ELF_hdr = region.begin();
83 
84   bool shdr_result = m_pELFReader->readSectionHeaders(pInput, ELF_hdr);
85 
86   // read .dynamic to get the correct SONAME
87   bool dyn_result = m_pELFReader->readDynamic(pInput);
88 
89   return (shdr_result && dyn_result);
90 }
91 
92 /// readSymbols
readSymbols(Input & pInput)93 bool ELFDynObjReader::readSymbols(Input& pInput) {
94   assert(pInput.hasMemArea());
95 
96   LDSection* symtab_shdr = pInput.context()->getSection(".dynsym");
97   if (symtab_shdr == NULL) {
98     note(diag::note_has_no_symtab) << pInput.name() << pInput.path()
99                                    << ".dynsym";
100     return true;
101   }
102 
103   LDSection* strtab_shdr = symtab_shdr->getLink();
104   if (strtab_shdr == NULL) {
105     fatal(diag::fatal_cannot_read_strtab) << pInput.name() << pInput.path()
106                                           << ".dynsym";
107     return false;
108   }
109 
110   llvm::StringRef symtab_region = pInput.memArea()->request(
111       pInput.fileOffset() + symtab_shdr->offset(), symtab_shdr->size());
112 
113   llvm::StringRef strtab_region = pInput.memArea()->request(
114       pInput.fileOffset() + strtab_shdr->offset(), strtab_shdr->size());
115   const char* strtab = strtab_region.begin();
116   bool result =
117       m_pELFReader->readSymbols(pInput, m_Builder, symtab_region, strtab);
118   return result;
119 }
120 
121 }  // namespace mcld
122