PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/frameworks/compile/mclinker/lib/LD/GNUArchiveReader.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 393 lines | 283 code | 55 blank | 55 comment | 53 complexity | be61a21a38fb02d380fd64e57ef33e2d MD5 | raw file
  1. //===- GNUArchiveReader.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/MC/MCLDInfo.h>
  10. #include <mcld/MC/MCLDInput.h>
  11. #include <mcld/MC/InputTree.h>
  12. #include <mcld/LD/GNUArchiveReader.h>
  13. #include <mcld/LD/ResolveInfo.h>
  14. #include <mcld/LD/ELFObjectReader.h>
  15. #include <mcld/Support/FileSystem.h>
  16. #include <mcld/Support/FileHandle.h>
  17. #include <mcld/Support/MemoryArea.h>
  18. #include <mcld/Support/MemoryRegion.h>
  19. #include <mcld/Support/MemoryAreaFactory.h>
  20. #include <mcld/Support/MsgHandling.h>
  21. #include <mcld/Support/Path.h>
  22. #include <mcld/ADT/SizeTraits.h>
  23. #include <llvm/ADT/StringRef.h>
  24. #include <llvm/Support/Host.h>
  25. #include <cstring>
  26. #include <cstdlib>
  27. using namespace mcld;
  28. GNUArchiveReader::GNUArchiveReader(MCLDInfo& pLDInfo,
  29. MemoryAreaFactory& pMemAreaFactory,
  30. ELFObjectReader& pELFObjectReader)
  31. : m_LDInfo(pLDInfo),
  32. m_MemAreaFactory(pMemAreaFactory),
  33. m_ELFObjectReader(pELFObjectReader)
  34. {
  35. }
  36. GNUArchiveReader::~GNUArchiveReader()
  37. {
  38. }
  39. /// isMyFormat
  40. bool GNUArchiveReader::isMyFormat(Input& pInput) const
  41. {
  42. assert(pInput.hasMemArea());
  43. MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(),
  44. Archive::MAGIC_LEN);
  45. const char* str = reinterpret_cast<const char*>(region->getBuffer());
  46. bool result = false;
  47. assert(NULL != str);
  48. if (isArchive(str) || isThinArchive(str))
  49. result = true;
  50. pInput.memArea()->release(region);
  51. return result;
  52. }
  53. /// isArchive
  54. bool GNUArchiveReader::isArchive(const char* pStr) const
  55. {
  56. return (0 == memcmp(pStr, Archive::MAGIC, Archive::MAGIC_LEN));
  57. }
  58. /// isThinArchive
  59. bool GNUArchiveReader::isThinArchive(const char* pStr) const
  60. {
  61. return (0 == memcmp(pStr, Archive::THIN_MAGIC, Archive::MAGIC_LEN));
  62. }
  63. /// isThinArchive
  64. bool GNUArchiveReader::isThinArchive(Input& pInput) const
  65. {
  66. assert(pInput.hasMemArea());
  67. MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(),
  68. Archive::MAGIC_LEN);
  69. const char* str = reinterpret_cast<const char*>(region->getBuffer());
  70. bool result = false;
  71. assert(NULL != str);
  72. if (isThinArchive(str))
  73. result = true;
  74. pInput.memArea()->release(region);
  75. return result;
  76. }
  77. bool GNUArchiveReader::readArchive(Archive& pArchive)
  78. {
  79. // read the symtab of the archive
  80. readSymbolTable(pArchive);
  81. // read the strtab of the archive
  82. readStringTable(pArchive);
  83. // add root archive to ArchiveMemberMap
  84. pArchive.addArchiveMember(pArchive.getARFile().name(),
  85. pArchive.inputs().root(),
  86. &InputTree::Downward);
  87. // include the needed members in the archive and build up the input tree
  88. bool willSymResolved;
  89. do {
  90. willSymResolved = false;
  91. for (size_t idx = 0; idx < pArchive.numOfSymbols(); ++idx) {
  92. // bypass if we already decided to include this symbol or not
  93. if (Archive::Symbol::Unknown != pArchive.getSymbolStatus(idx))
  94. continue;
  95. // bypass if another symbol with the same object file offset is included
  96. if (pArchive.hasObjectMember(pArchive.getObjFileOffset(idx))) {
  97. pArchive.setSymbolStatus(idx, Archive::Symbol::Include);
  98. continue;
  99. }
  100. // check if we should include this defined symbol
  101. Archive::Symbol::Status status =
  102. shouldIncludeSymbol(pArchive.getSymbolName(idx));
  103. if (Archive::Symbol::Unknown != status)
  104. pArchive.setSymbolStatus(idx, status);
  105. if (Archive::Symbol::Include == status) {
  106. Input* cur_archive = &(pArchive.getARFile());
  107. Input* member = cur_archive;
  108. uint32_t file_offset = pArchive.getObjFileOffset(idx);
  109. while ((member != NULL) && (Input::Object != member->type())) {
  110. uint32_t nested_offset = 0;
  111. // use the file offset in current archive to find out the member we
  112. // want to include
  113. member = readMemberHeader(pArchive,
  114. *cur_archive,
  115. file_offset,
  116. nested_offset);
  117. assert(member != NULL);
  118. // bypass if we get an archive that is already in the map
  119. if (Input::Archive == member->type()) {
  120. cur_archive = member;
  121. file_offset = nested_offset;
  122. continue;
  123. }
  124. // insert a node into the subtree of current archive.
  125. Archive::ArchiveMember* parent =
  126. pArchive.getArchiveMember(cur_archive->name());
  127. assert(NULL != parent);
  128. pArchive.inputs().insert(parent->lastPos, *(parent->move), *member);
  129. // move the iterator to new created node, and also adjust the
  130. // direction to Afterward for next insertion in this subtree
  131. parent->move->move(parent->lastPos);
  132. parent->move = &InputTree::Afterward;
  133. if (m_ELFObjectReader.isMyFormat(*member)) {
  134. member->setType(Input::Object);
  135. pArchive.addObjectMember(pArchive.getObjFileOffset(idx),
  136. parent->lastPos);
  137. m_ELFObjectReader.readObject(*member);
  138. m_ELFObjectReader.readSections(*member);
  139. m_ELFObjectReader.readSymbols(*member);
  140. }
  141. else if (isMyFormat(*member)) {
  142. member->setType(Input::Archive);
  143. // when adding a new archive node, set the iterator to archive
  144. // itself, and set the direction to Downward
  145. pArchive.addArchiveMember(member->name(),
  146. parent->lastPos,
  147. &InputTree::Downward);
  148. cur_archive = member;
  149. file_offset = nested_offset;
  150. }
  151. } // end of while
  152. willSymResolved = true;
  153. } // end of if
  154. } // end of for
  155. } while (willSymResolved);
  156. return true;
  157. }
  158. /// readMemberHeader - read the header of a member in a archive file and then
  159. /// return the corresponding archive member (it may be an input object or
  160. /// another archive)
  161. /// @param pArchiveRoot - the archive root that holds the strtab (extended
  162. /// name table)
  163. /// @param pArchiveFile - the archive that contains the needed object
  164. /// @param pFileOffset - file offset of the member header in the archive
  165. /// @param pNestedOffset - used when we find a nested archive
  166. Input* GNUArchiveReader::readMemberHeader(Archive& pArchiveRoot,
  167. Input& pArchiveFile,
  168. uint32_t pFileOffset,
  169. uint32_t& pNestedOffset)
  170. {
  171. assert(pArchiveFile.hasMemArea());
  172. MemoryRegion* header_region =
  173. pArchiveFile.memArea()->request((pArchiveFile.fileOffset() + pFileOffset),
  174. sizeof(Archive::MemberHeader));
  175. const Archive::MemberHeader* header =
  176. reinterpret_cast<const Archive::MemberHeader*>(header_region->getBuffer());
  177. assert(0 == memcmp(header->fmag, Archive::MEMBER_MAGIC, 2));
  178. // int size = atoi(header->size);
  179. // parse the member name and nested offset if any
  180. std::string member_name;
  181. llvm::StringRef name_field(header->name, 16);
  182. if ('/' != header->name[0]) {
  183. // this is an object file in an archive
  184. size_t pos = name_field.find_first_of('/');
  185. member_name.assign(name_field.substr(0, pos).str());
  186. }
  187. else {
  188. // this is an object/archive file in a thin archive
  189. size_t begin = 1;
  190. size_t end = name_field.find_first_of(" :");
  191. uint32_t name_offset = 0;
  192. // parse the name offset
  193. name_field.substr(begin, end - begin).getAsInteger(10, name_offset);
  194. if (':' == name_field[end]) {
  195. // there is a nested offset
  196. begin = end + 1;
  197. end = name_field.find_first_of(' ', begin);
  198. name_field.substr(begin, end - begin).getAsInteger(10, pNestedOffset);
  199. }
  200. // get the member name from the extended name table
  201. begin = name_offset;
  202. end = pArchiveRoot.getStrTable().find_first_of('\n', begin);
  203. member_name.assign(pArchiveRoot.getStrTable().substr(begin, end - begin -1));
  204. }
  205. Input* member = NULL;
  206. if (!isThinArchive(pArchiveFile)) {
  207. // this is an object file in an archive
  208. member =
  209. m_LDInfo.inputFactory().produce(member_name,
  210. pArchiveFile.path(),
  211. Input::Unknown,
  212. (pFileOffset +
  213. sizeof(Archive::MemberHeader)));
  214. assert(member != NULL);
  215. member->setMemArea(pArchiveFile.memArea());
  216. LDContext *input_context = m_LDInfo.contextFactory().produce();
  217. member->setContext(input_context);
  218. }
  219. else {
  220. // this is a member in a thin archive
  221. // try to find if this is a archive already in the map first
  222. Archive::ArchiveMember* ar_member =
  223. pArchiveRoot.getArchiveMember(member_name);
  224. if (NULL != ar_member) {
  225. return ar_member->file;
  226. }
  227. // get nested file path, the nested file's member name is the relative
  228. // path to the archive containing it.
  229. sys::fs::Path input_path(pArchiveFile.path().parent_path());
  230. if (!input_path.empty())
  231. input_path.append(member_name);
  232. else
  233. input_path.assign(member_name);
  234. member =
  235. m_LDInfo.inputFactory().produce(member_name, input_path, Input::Unknown);
  236. assert(member != NULL);
  237. MemoryArea* input_memory =
  238. m_MemAreaFactory.produce(member->path(), FileHandle::ReadOnly);
  239. if (input_memory->handler()->isGood()) {
  240. member->setMemArea(input_memory);
  241. }
  242. else {
  243. error(diag::err_cannot_open_input) << member->name() << member->path();
  244. return NULL;
  245. }
  246. LDContext *input_context = m_LDInfo.contextFactory().produce(input_path);
  247. member->setContext(input_context);
  248. }
  249. pArchiveFile.memArea()->release(header_region);
  250. return member;
  251. }
  252. /// readSymbolTable - read the archive symbol map (armap)
  253. bool GNUArchiveReader::readSymbolTable(Archive& pArchive)
  254. {
  255. assert(pArchive.getARFile().hasMemArea());
  256. MemoryRegion* header_region =
  257. pArchive.getARFile().memArea()->request((pArchive.getARFile().fileOffset() +
  258. Archive::MAGIC_LEN),
  259. sizeof(Archive::MemberHeader));
  260. const Archive::MemberHeader* header =
  261. reinterpret_cast<const Archive::MemberHeader*>(header_region->getBuffer());
  262. assert(0 == memcmp(header->fmag, Archive::MEMBER_MAGIC, 2));
  263. int symtab_size = atoi(header->size);
  264. pArchive.setSymTabSize(symtab_size);
  265. MemoryRegion* symtab_region =
  266. pArchive.getARFile().memArea()->request((pArchive.getARFile().fileOffset() +
  267. Archive::MAGIC_LEN +
  268. sizeof(Archive::MemberHeader)),
  269. symtab_size);
  270. const uint32_t* data =
  271. reinterpret_cast<const uint32_t*>(symtab_region->getBuffer());
  272. // read the number of symbols
  273. uint32_t number = 0;
  274. if (llvm::sys::isLittleEndianHost())
  275. number = bswap32(*data);
  276. else
  277. number = *data;
  278. // set up the pointers for file offset and name offset
  279. ++data;
  280. const char* name = reinterpret_cast<const char*>(data + number);
  281. // add the archive symbols
  282. for (uint32_t i = 0; i < number; ++i) {
  283. if (llvm::sys::isLittleEndianHost())
  284. pArchive.addSymbol(name, bswap32(*data));
  285. else
  286. pArchive.addSymbol(name, *data);
  287. name += strlen(name) + 1;
  288. ++data;
  289. }
  290. pArchive.getARFile().memArea()->release(header_region);
  291. pArchive.getARFile().memArea()->release(symtab_region);
  292. return true;
  293. }
  294. /// readStringTable - read the strtab for long file name of the archive
  295. bool GNUArchiveReader::readStringTable(Archive& pArchive)
  296. {
  297. size_t offset = Archive::MAGIC_LEN +
  298. sizeof(Archive::MemberHeader) +
  299. pArchive.getSymTabSize();
  300. if (0x0 != (offset & 1))
  301. ++offset;
  302. assert(pArchive.getARFile().hasMemArea());
  303. MemoryRegion* header_region =
  304. pArchive.getARFile().memArea()->request((pArchive.getARFile().fileOffset() +
  305. offset),
  306. sizeof(Archive::MemberHeader));
  307. const Archive::MemberHeader* header =
  308. reinterpret_cast<const Archive::MemberHeader*>(header_region->getBuffer());
  309. assert(0 == memcmp(header->fmag, Archive::MEMBER_MAGIC, 2));
  310. int strtab_size = atoi(header->size);
  311. MemoryRegion* strtab_region =
  312. pArchive.getARFile().memArea()->request((pArchive.getARFile().fileOffset() +
  313. offset +
  314. sizeof(Archive::MemberHeader)),
  315. strtab_size);
  316. const char* strtab =
  317. reinterpret_cast<const char*>(strtab_region->getBuffer());
  318. pArchive.getStrTable().assign(strtab, strtab_size);
  319. pArchive.getARFile().memArea()->release(header_region);
  320. pArchive.getARFile().memArea()->release(strtab_region);
  321. return true;
  322. }
  323. /// shouldIncludeStatus - given a sym name from armap and check if including
  324. /// the corresponding archive member, and then return the decision
  325. enum Archive::Symbol::Status
  326. GNUArchiveReader::shouldIncludeSymbol(const llvm::StringRef& pSymName) const
  327. {
  328. // TODO: handle symbol version issue and user defined symbols
  329. ResolveInfo* info = m_LDInfo.getNamePool().findInfo(pSymName);
  330. if (NULL != info) {
  331. if (!info->isUndef())
  332. return Archive::Symbol::Exclude;
  333. if (info->isWeak())
  334. return Archive::Symbol::Unknown;
  335. return Archive::Symbol::Include;
  336. }
  337. return Archive::Symbol::Unknown;
  338. }