PageRenderTime 75ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/frameworks/compile/mclinker/lib/MC/MCLinker.cpp

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C++ | 718 lines | 481 code | 105 blank | 132 comment | 107 complexity | eb29ced25ff9d88aa529943840c3e351 MD5 | raw file
  1. //===- MCLinker.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. //
  10. // This file implements the MCLinker class
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include <mcld/MC/MCLinker.h>
  14. #include <llvm/Support/Host.h>
  15. #include <llvm/Support/raw_ostream.h>
  16. #include <mcld/MC/MCLDInput.h>
  17. #include <mcld/MC/MCLDInfo.h>
  18. #include <mcld/LD/Resolver.h>
  19. #include <mcld/LD/LDContext.h>
  20. #include <mcld/LD/LDSymbol.h>
  21. #include <mcld/LD/LDSectionFactory.h>
  22. #include <mcld/LD/SectionMap.h>
  23. #include <mcld/LD/RelocationFactory.h>
  24. #include <mcld/LD/FillFragment.h>
  25. #include <mcld/LD/RegionFragment.h>
  26. #include <mcld/LD/EhFrame.h>
  27. #include <mcld/LD/EhFrameHdr.h>
  28. #include <mcld/Support/MemoryRegion.h>
  29. #include <mcld/Support/MsgHandling.h>
  30. #include <mcld/Target/TargetLDBackend.h>
  31. using namespace mcld;
  32. /// Constructor
  33. MCLinker::MCLinker(TargetLDBackend& pBackend,
  34. MCLDInfo& pInfo,
  35. SectionMap& pSectionMap)
  36. : m_Backend(pBackend),
  37. m_LDInfo(pInfo),
  38. m_SectionMap(pSectionMap),
  39. m_LDSymbolFactory(128),
  40. m_LDSectHdrFactory(10), // the average number of sections. (assuming 10.)
  41. m_LDSectDataFactory(10),
  42. m_pSectionMerger(NULL)
  43. {
  44. }
  45. /// Destructor
  46. MCLinker::~MCLinker()
  47. {
  48. if (NULL != m_pSectionMerger)
  49. delete m_pSectionMerger;
  50. }
  51. //===----------------------------------------------------------------------===//
  52. // Symbol Operations
  53. //===----------------------------------------------------------------------===//
  54. /// addSymbolFromObject - add a symbol from object file and resolve it
  55. /// immediately
  56. LDSymbol* MCLinker::addSymbolFromObject(const llvm::StringRef& pName,
  57. ResolveInfo::Type pType,
  58. ResolveInfo::Desc pDesc,
  59. ResolveInfo::Binding pBinding,
  60. ResolveInfo::SizeType pSize,
  61. LDSymbol::ValueType pValue,
  62. FragmentRef* pFragmentRef,
  63. ResolveInfo::Visibility pVisibility)
  64. {
  65. // resolved_result is a triple <resolved_info, existent, override>
  66. Resolver::Result resolved_result;
  67. ResolveInfo old_info; // used for arrange output symbols
  68. if (pBinding == ResolveInfo::Local) {
  69. // if the symbol is a local symbol, create a LDSymbol for input, but do not
  70. // resolve them.
  71. resolved_result.info = m_LDInfo.getNamePool().createSymbol(pName,
  72. false,
  73. pType,
  74. pDesc,
  75. pBinding,
  76. pSize,
  77. pVisibility);
  78. // No matter if there is a symbol with the same name, insert the symbol
  79. // into output symbol table. So, we let the existent false.
  80. resolved_result.existent = false;
  81. resolved_result.overriden = true;
  82. }
  83. else {
  84. // if the symbol is not local, insert and resolve it immediately
  85. m_LDInfo.getNamePool().insertSymbol(pName, false, pType, pDesc, pBinding,
  86. pSize, pVisibility,
  87. &old_info, resolved_result);
  88. }
  89. // the return ResolveInfo should not NULL
  90. assert(NULL != resolved_result.info);
  91. // create a LDSymbol for the input file.
  92. LDSymbol* input_sym = m_LDSymbolFactory.allocate();
  93. new (input_sym) LDSymbol();
  94. // set the relation between input LDSymbol and its ResolveInfo
  95. input_sym->setResolveInfo(*resolved_result.info);
  96. // set up input LDSymbol
  97. input_sym->setFragmentRef(pFragmentRef);
  98. input_sym->setValue(pValue);
  99. LDSymbol* output_sym = resolved_result.info->outSymbol();
  100. bool has_output_sym = (NULL != output_sym);
  101. if (!resolved_result.existent || !has_output_sym) {
  102. // it is a new symbol, the output_sym should be NULL.
  103. assert(NULL == output_sym);
  104. // if it is a new symbol, create a LDSymbol for the output
  105. output_sym = m_LDSymbolFactory.allocate();
  106. new (output_sym) LDSymbol();
  107. // set up the relation between output LDSymbol and its ResolveInfo
  108. output_sym->setResolveInfo(*resolved_result.info);
  109. resolved_result.info->setSymPtr(output_sym);
  110. }
  111. if (resolved_result.overriden || !has_output_sym) {
  112. // symbol can be overriden only if it exists.
  113. assert(output_sym != NULL);
  114. // should override output LDSymbol
  115. output_sym->setFragmentRef(pFragmentRef);
  116. output_sym->setValue(pValue);
  117. }
  118. // After symbol resolution, visibility is changed to the most restrict one.
  119. // we need to arrange its position in the output symbol .
  120. if (pType != ResolveInfo::Section) {
  121. if (!has_output_sym) {
  122. // We merge sections when reading them. So we do not need to output symbols
  123. // with section type
  124. // No matter the symbol is already in the output or not, add it if it
  125. // should be forcefully set local.
  126. if (shouldForceLocal(*resolved_result.info))
  127. m_OutputSymbols.forceLocal(*output_sym);
  128. else {
  129. // the symbol should not be forcefully local.
  130. m_OutputSymbols.add(*output_sym);
  131. }
  132. }
  133. else if (resolved_result.overriden) {
  134. if (!shouldForceLocal(old_info) ||
  135. !shouldForceLocal(*resolved_result.info)) {
  136. // If the old info and the new info are both forcefully local, then
  137. // we should keep the output_sym in forcefully local category. Else,
  138. // we should re-sort the output_sym
  139. m_OutputSymbols.arrange(*output_sym, old_info);
  140. }
  141. }
  142. }
  143. return input_sym;
  144. }
  145. /// addSymbolFromDynObj - add a symbol from object file and resolve it
  146. /// immediately
  147. LDSymbol* MCLinker::addSymbolFromDynObj(const llvm::StringRef& pName,
  148. ResolveInfo::Type pType,
  149. ResolveInfo::Desc pDesc,
  150. ResolveInfo::Binding pBinding,
  151. ResolveInfo::SizeType pSize,
  152. LDSymbol::ValueType pValue,
  153. FragmentRef* pFragmentRef,
  154. ResolveInfo::Visibility pVisibility)
  155. {
  156. // We merge sections when reading them. So we do not need symbols with
  157. // section type
  158. if (pType == ResolveInfo::Section)
  159. return NULL;
  160. // ignore symbols with local binding or that have internal or hidden
  161. // visibility
  162. if (pBinding == ResolveInfo::Local ||
  163. pVisibility == ResolveInfo::Internal ||
  164. pVisibility == ResolveInfo::Hidden)
  165. return NULL;
  166. // A protected symbol in a shared library must be treated as a
  167. // normal symbol when viewed from outside the shared library.
  168. if (pVisibility == ResolveInfo::Protected)
  169. pVisibility = ResolveInfo::Default;
  170. // insert symbol and resolve it immediately
  171. // resolved_result is a triple <resolved_info, existent, override>
  172. Resolver::Result resolved_result;
  173. m_LDInfo.getNamePool().insertSymbol(pName, true, pType, pDesc,
  174. pBinding, pSize, pVisibility,
  175. NULL, resolved_result);
  176. // the return ResolveInfo should not NULL
  177. assert(NULL != resolved_result.info);
  178. // create a LDSymbol for the input file.
  179. LDSymbol* input_sym = m_LDSymbolFactory.allocate();
  180. new (input_sym) LDSymbol();
  181. // set up the relation between input LDSymbol and its ResolveInfo
  182. input_sym->setResolveInfo(*resolved_result.info);
  183. // set up input LDSymbol
  184. input_sym->setFragmentRef(pFragmentRef);
  185. input_sym->setValue(pValue);
  186. LDSymbol* output_sym = NULL;
  187. if (!resolved_result.existent) {
  188. // we get a new symbol, leave it as NULL
  189. resolved_result.info->setSymPtr(NULL);
  190. }
  191. else {
  192. // we saw the symbol before, but the output_sym still may be NULL.
  193. output_sym = resolved_result.info->outSymbol();
  194. }
  195. if (output_sym != NULL) {
  196. // After symbol resolution, visibility is changed to the most restrict one.
  197. // If we are not doing incremental linking, then any symbol with hidden
  198. // or internal visibility is forcefully set as a local symbol.
  199. if (shouldForceLocal(*resolved_result.info)) {
  200. m_OutputSymbols.forceLocal(*output_sym);
  201. }
  202. }
  203. return input_sym;
  204. }
  205. /// defineSymbolForcefully - define an output symbol and override it immediately
  206. LDSymbol* MCLinker::defineSymbolForcefully(const llvm::StringRef& pName,
  207. bool pIsDyn,
  208. ResolveInfo::Type pType,
  209. ResolveInfo::Desc pDesc,
  210. ResolveInfo::Binding pBinding,
  211. ResolveInfo::SizeType pSize,
  212. LDSymbol::ValueType pValue,
  213. FragmentRef* pFragmentRef,
  214. ResolveInfo::Visibility pVisibility)
  215. {
  216. ResolveInfo* info = m_LDInfo.getNamePool().findInfo(pName);
  217. LDSymbol* output_sym = NULL;
  218. if (NULL == info) {
  219. // the symbol is not in the pool, create a new one.
  220. // create a ResolveInfo
  221. Resolver::Result result;
  222. m_LDInfo.getNamePool().insertSymbol(pName, pIsDyn, pType, pDesc,
  223. pBinding, pSize, pVisibility,
  224. NULL, result);
  225. assert(!result.existent);
  226. // create a output LDSymbol
  227. output_sym = m_LDSymbolFactory.allocate();
  228. new (output_sym) LDSymbol();
  229. output_sym->setResolveInfo(*result.info);
  230. result.info->setSymPtr(output_sym);
  231. if (shouldForceLocal(*result.info))
  232. m_OutputSymbols.forceLocal(*output_sym);
  233. else
  234. m_OutputSymbols.add(*output_sym);
  235. }
  236. else {
  237. // the symbol is already in the pool, override it
  238. ResolveInfo old_info;
  239. old_info.override(*info);
  240. info->setSource(pIsDyn);
  241. info->setType(pType);
  242. info->setDesc(pDesc);
  243. info->setBinding(pBinding);
  244. info->setVisibility(pVisibility);
  245. info->setIsSymbol(true);
  246. info->setSize(pSize);
  247. output_sym = info->outSymbol();
  248. if (NULL != output_sym)
  249. m_OutputSymbols.arrange(*output_sym, old_info);
  250. else {
  251. // create a output LDSymbol
  252. output_sym = m_LDSymbolFactory.allocate();
  253. new (output_sym) LDSymbol();
  254. output_sym->setResolveInfo(*info);
  255. info->setSymPtr(output_sym);
  256. m_OutputSymbols.add(*output_sym);
  257. }
  258. }
  259. if (NULL != output_sym) {
  260. output_sym->setFragmentRef(pFragmentRef);
  261. output_sym->setValue(pValue);
  262. }
  263. return output_sym;
  264. }
  265. /// defineSymbolAsRefered - define an output symbol and override it immediately
  266. LDSymbol* MCLinker::defineSymbolAsRefered(const llvm::StringRef& pName,
  267. bool pIsDyn,
  268. ResolveInfo::Type pType,
  269. ResolveInfo::Desc pDesc,
  270. ResolveInfo::Binding pBinding,
  271. ResolveInfo::SizeType pSize,
  272. LDSymbol::ValueType pValue,
  273. FragmentRef* pFragmentRef,
  274. ResolveInfo::Visibility pVisibility)
  275. {
  276. ResolveInfo* info = m_LDInfo.getNamePool().findInfo(pName);
  277. if (NULL == info || !(info->isUndef() || info->isDyn())) {
  278. // only undefined symbol and dynamic symbol can make a reference.
  279. return NULL;
  280. }
  281. // the symbol is already in the pool, override it
  282. ResolveInfo old_info;
  283. old_info.override(*info);
  284. info->setSource(pIsDyn);
  285. info->setType(pType);
  286. info->setDesc(pDesc);
  287. info->setBinding(pBinding);
  288. info->setVisibility(pVisibility);
  289. info->setIsSymbol(true);
  290. info->setSize(pSize);
  291. LDSymbol* output_sym = info->outSymbol();
  292. if (NULL != output_sym) {
  293. output_sym->setFragmentRef(pFragmentRef);
  294. output_sym->setValue(pValue);
  295. m_OutputSymbols.arrange(*output_sym, old_info);
  296. }
  297. else {
  298. // create a output LDSymbol
  299. output_sym = m_LDSymbolFactory.allocate();
  300. new (output_sym) LDSymbol();
  301. output_sym->setResolveInfo(*info);
  302. info->setSymPtr(output_sym);
  303. m_OutputSymbols.add(*output_sym);
  304. }
  305. return output_sym;
  306. }
  307. /// defineAndResolveSymbolForcefully - define an output symbol and resolve it
  308. /// immediately
  309. LDSymbol* MCLinker::defineAndResolveSymbolForcefully(const llvm::StringRef& pName,
  310. bool pIsDyn,
  311. ResolveInfo::Type pType,
  312. ResolveInfo::Desc pDesc,
  313. ResolveInfo::Binding pBinding,
  314. ResolveInfo::SizeType pSize,
  315. LDSymbol::ValueType pValue,
  316. FragmentRef* pFragmentRef,
  317. ResolveInfo::Visibility pVisibility)
  318. {
  319. // Result is <info, existent, override>
  320. Resolver::Result result;
  321. ResolveInfo old_info;
  322. m_LDInfo.getNamePool().insertSymbol(pName, pIsDyn, pType, pDesc, pBinding,
  323. pSize, pVisibility,
  324. &old_info, result);
  325. LDSymbol* output_sym = result.info->outSymbol();
  326. bool has_output_sym = (NULL != output_sym);
  327. if (!result.existent || !has_output_sym) {
  328. output_sym = m_LDSymbolFactory.allocate();
  329. new (output_sym) LDSymbol();
  330. output_sym->setResolveInfo(*result.info);
  331. result.info->setSymPtr(output_sym);
  332. }
  333. if (result.overriden || !has_output_sym) {
  334. output_sym->setFragmentRef(pFragmentRef);
  335. output_sym->setValue(pValue);
  336. }
  337. // After symbol resolution, the visibility is changed to the most restrict.
  338. // arrange the output position
  339. if (shouldForceLocal(*result.info))
  340. m_OutputSymbols.forceLocal(*output_sym);
  341. else if (has_output_sym)
  342. m_OutputSymbols.arrange(*output_sym, old_info);
  343. else
  344. m_OutputSymbols.add(*output_sym);
  345. return output_sym;
  346. }
  347. /// defineAndResolveSymbolAsRefered - define an output symbol and resolve it
  348. /// immediately.
  349. LDSymbol* MCLinker::defineAndResolveSymbolAsRefered(const llvm::StringRef& pName,
  350. bool pIsDyn,
  351. ResolveInfo::Type pType,
  352. ResolveInfo::Desc pDesc,
  353. ResolveInfo::Binding pBinding,
  354. ResolveInfo::SizeType pSize,
  355. LDSymbol::ValueType pValue,
  356. FragmentRef* pFragmentRef,
  357. ResolveInfo::Visibility pVisibility)
  358. {
  359. ResolveInfo* info = m_LDInfo.getNamePool().findInfo(pName);
  360. if (NULL == info || !(info->isUndef() || info->isDyn())) {
  361. // only undefined symbol and dynamic symbol can make a reference.
  362. return NULL;
  363. }
  364. return defineAndResolveSymbolForcefully(pName,
  365. pIsDyn,
  366. pType,
  367. pDesc,
  368. pBinding,
  369. pSize,
  370. pValue,
  371. pFragmentRef,
  372. pVisibility);
  373. }
  374. bool MCLinker::finalizeSymbols()
  375. {
  376. SymbolCategory::iterator symbol, symEnd = m_OutputSymbols.end();
  377. for (symbol = m_OutputSymbols.begin(); symbol != symEnd; ++symbol) {
  378. if ((*symbol)->resolveInfo()->isAbsolute() ||
  379. (*symbol)->resolveInfo()->type() == ResolveInfo::File) {
  380. // absolute symbols or symbols with function type should have
  381. // zero value
  382. (*symbol)->setValue(0x0);
  383. continue;
  384. }
  385. if ((*symbol)->hasFragRef()) {
  386. // set the virtual address of the symbol. If the output file is
  387. // relocatable object file, the section's virtual address becomes zero.
  388. // And the symbol's value become section relative offset.
  389. uint64_t value = getLayout().getOutputOffset(*(*symbol)->fragRef());
  390. assert(NULL != (*symbol)->fragRef()->frag());
  391. uint64_t addr = getLayout().getOutputLDSection(*(*symbol)->fragRef()->frag())->addr();
  392. (*symbol)->setValue(value + addr);
  393. continue;
  394. }
  395. }
  396. // finialize target-dependent symbols
  397. return m_Backend.finalizeSymbols(*this, m_LDInfo.output());
  398. }
  399. bool MCLinker::shouldForceLocal(const ResolveInfo& pInfo) const
  400. {
  401. // forced local symbol matches all rules:
  402. // 1. We are not doing incremental linking.
  403. // 2. The symbol is with Hidden or Internal visibility.
  404. // 3. The symbol should be global or weak. Otherwise, local symbol is local.
  405. // 4. The symbol is defined or common
  406. if (m_LDInfo.output().type() != Output::Object &&
  407. (pInfo.visibility() == ResolveInfo::Hidden ||
  408. pInfo.visibility() == ResolveInfo::Internal) &&
  409. (pInfo.isGlobal() || pInfo.isWeak()) &&
  410. (pInfo.isDefine() || pInfo.isCommon()))
  411. return true;
  412. return false;
  413. }
  414. //===----------------------------------------------------------------------===//
  415. // Section Operations
  416. //===----------------------------------------------------------------------===//
  417. /// createSectHdr - create the input section header
  418. LDSection& MCLinker::createSectHdr(const std::string& pName,
  419. LDFileFormat::Kind pKind,
  420. uint32_t pType,
  421. uint32_t pFlag)
  422. {
  423. assert(m_LDInfo.output().hasContext());
  424. // for user such as reader, standard/target fromat
  425. LDSection* result =
  426. m_LDSectHdrFactory.produce(pName, pKind, pType, pFlag);
  427. // check if we need to create a output section for output LDContext
  428. std::string sect_name = m_SectionMap.getOutputSectName(pName);
  429. LDSection* output_sect = m_LDInfo.output().context()->getSection(sect_name);
  430. if (NULL == output_sect) {
  431. // create a output section and push it into output LDContext
  432. output_sect =
  433. m_LDSectHdrFactory.produce(sect_name, pKind, pType, pFlag);
  434. m_LDInfo.output().context()->getSectionTable().push_back(output_sect);
  435. m_pSectionMerger->addMapping(pName, output_sect);
  436. }
  437. return *result;
  438. }
  439. /// getOrCreateOutputSectHdr - for reader and standard/target format to get
  440. /// or create the output's section header
  441. LDSection& MCLinker::getOrCreateOutputSectHdr(const std::string& pName,
  442. LDFileFormat::Kind pKind,
  443. uint32_t pType,
  444. uint32_t pFlag,
  445. uint32_t pAlign)
  446. {
  447. assert(m_LDInfo.output().hasContext());
  448. // check if we need to create a output section for output LDContext
  449. std::string sect_name = m_SectionMap.getOutputSectName(pName);
  450. LDSection* output_sect = m_LDInfo.output().context()->getSection(sect_name);
  451. if (NULL == output_sect) {
  452. // create a output section and push it into output LDContext
  453. output_sect =
  454. m_LDSectHdrFactory.produce(sect_name, pKind, pType, pFlag);
  455. output_sect->setAlign(pAlign);
  456. m_LDInfo.output().context()->getSectionTable().push_back(output_sect);
  457. m_pSectionMerger->addMapping(pName, output_sect);
  458. }
  459. return *output_sect;
  460. }
  461. /// getOrCreateSectData - get or create SectionData
  462. /// pSection is input LDSection
  463. SectionData& MCLinker::getOrCreateSectData(LDSection& pSection)
  464. {
  465. // if there is already a section data pointed by section, return it.
  466. SectionData* sect_data = pSection.getSectionData();
  467. if (NULL != sect_data) {
  468. m_Layout.addInputRange(*sect_data, pSection);
  469. return *sect_data;
  470. }
  471. // try to get one from output LDSection
  472. LDSection* output_sect =
  473. m_pSectionMerger->getOutputSectHdr(pSection.name());
  474. assert(NULL != output_sect);
  475. sect_data = output_sect->getSectionData();
  476. if (NULL != sect_data) {
  477. pSection.setSectionData(sect_data);
  478. m_Layout.addInputRange(*sect_data, pSection);
  479. return *sect_data;
  480. }
  481. // if the output LDSection also has no SectionData, then create one.
  482. sect_data = m_LDSectDataFactory.allocate();
  483. new (sect_data) SectionData(*output_sect);
  484. pSection.setSectionData(sect_data);
  485. output_sect->setSectionData(sect_data);
  486. m_Layout.addInputRange(*sect_data, pSection);
  487. return *sect_data;
  488. }
  489. void MCLinker::initSectionMap()
  490. {
  491. assert(m_LDInfo.output().hasContext());
  492. if (NULL == m_pSectionMerger)
  493. m_pSectionMerger = new SectionMerger(m_SectionMap, *m_LDInfo.output().context());
  494. }
  495. bool MCLinker::layout()
  496. {
  497. return m_Layout.layout(m_LDInfo.output(), m_Backend, m_LDInfo);
  498. }
  499. //===----------------------------------------------------------------------===//
  500. // Relocation Operations
  501. //===----------------------------------------------------------------------===//
  502. /// addRelocation - add a relocation entry in MCLinker (only for object file)
  503. ///
  504. /// All symbols should be read and resolved before calling this function.
  505. Relocation* MCLinker::addRelocation(Relocation::Type pType,
  506. const LDSymbol& pSym,
  507. ResolveInfo& pResolveInfo,
  508. FragmentRef& pFragmentRef,
  509. const LDSection& pSection,
  510. Relocation::Address pAddend)
  511. {
  512. // FIXME: we should dicard sections and symbols first instead
  513. // if the symbol is in the discarded input section, then we also need to
  514. // discard this relocation.
  515. if (pSym.fragRef() == NULL &&
  516. pResolveInfo.type() == ResolveInfo::Section &&
  517. pResolveInfo.desc() == ResolveInfo::Undefined)
  518. return NULL;
  519. Relocation* relocation = m_Backend.getRelocFactory()->produce(pType,
  520. pFragmentRef,
  521. pAddend);
  522. relocation->setSymInfo(&pResolveInfo);
  523. m_RelocationList.push_back(relocation);
  524. m_Backend.scanRelocation(*relocation, pSym, *this, m_LDInfo,
  525. m_LDInfo.output(), pSection);
  526. if (pResolveInfo.isUndef() && !pResolveInfo.isDyn() && !pResolveInfo.isWeak())
  527. fatal(diag::undefined_reference) << pResolveInfo.name();
  528. return relocation;
  529. }
  530. bool MCLinker::applyRelocations()
  531. {
  532. RelocationListType::iterator relocIter, relocEnd = m_RelocationList.end();
  533. for (relocIter = m_RelocationList.begin(); relocIter != relocEnd; ++relocIter) {
  534. Fragment* frag = (Fragment*)relocIter;
  535. static_cast<Relocation*>(frag)->apply(*m_Backend.getRelocFactory(), m_LDInfo);
  536. }
  537. return true;
  538. }
  539. void MCLinker::syncRelocationResult()
  540. {
  541. MemoryRegion* region = m_LDInfo.output().memArea()->request(0,
  542. m_LDInfo.output().memArea()->handler()->size());
  543. uint8_t* data = region->getBuffer();
  544. RelocationListType::iterator relocIter, relocEnd = m_RelocationList.end();
  545. for (relocIter = m_RelocationList.begin(); relocIter != relocEnd; ++relocIter) {
  546. Fragment* frag = (Fragment*)relocIter;
  547. Relocation* reloc = static_cast<Relocation*>(frag);
  548. // get output file offset
  549. size_t out_offset = m_Layout.getOutputLDSection(*reloc->targetRef().frag())->offset() +
  550. m_Layout.getOutputOffset(reloc->targetRef());
  551. uint8_t* target_addr = data + out_offset;
  552. // byte swapping if target and host has different endian, and then write back
  553. if(llvm::sys::isLittleEndianHost() != m_Backend.isLittleEndian()) {
  554. uint64_t tmp_data = 0;
  555. switch(m_Backend.bitclass()) {
  556. case 32u:
  557. tmp_data = bswap32(reloc->target());
  558. std::memcpy(target_addr, &tmp_data, 4);
  559. break;
  560. case 64u:
  561. tmp_data = bswap64(reloc->target());
  562. std::memcpy(target_addr, &tmp_data, 8);
  563. break;
  564. default:
  565. break;
  566. }
  567. }
  568. else {
  569. std::memcpy(target_addr, &reloc->target(), m_Backend.bitclass()/8);
  570. }
  571. } // end of for
  572. m_LDInfo.output().memArea()->clear();
  573. }
  574. //===----------------------------------------------------------------------===//
  575. // Exception Handling Operations
  576. //===----------------------------------------------------------------------===//
  577. /// addEhFrame - add an exception handling section
  578. /// @param pSection - the input section
  579. /// @param pArea - the memory area which pSection is within.
  580. uint64_t MCLinker::addEhFrame(const Input& pInput,
  581. LDSection& pSection,
  582. MemoryArea& pArea)
  583. {
  584. uint64_t size = 0;
  585. // get the SectionData of this eh_frame
  586. SectionData& sect_data = getOrCreateSectData(pSection);
  587. // parse the eh_frame if the option --eh-frame-hdr is given
  588. if (m_LDInfo.options().hasEhFrameHdr()) {
  589. EhFrame* ehframe = m_Backend.getEhFrame();
  590. assert(NULL != ehframe);
  591. if (ehframe->canRecognizeAllEhFrame()) {
  592. size = ehframe->readEhFrame(m_Layout, m_Backend, sect_data, pInput,
  593. pSection, pArea);
  594. // zero size indicate that this is an empty section or we can't recognize
  595. // this eh_frame, handle it as a regular section.
  596. if (0 != size)
  597. return size;
  598. }
  599. }
  600. // handle eh_frame as a regular section
  601. MemoryRegion* region = pArea.request(pInput.fileOffset() + pSection.offset(),
  602. pSection.size());
  603. Fragment* frag = NULL;
  604. if (NULL == region) {
  605. // If the input section's size is zero, we got a NULL region.
  606. // use a virtual fill fragment
  607. frag = new FillFragment(0x0, 0, 0);
  608. }
  609. else
  610. frag = new RegionFragment(*region);
  611. size = m_Layout.appendFragment(*frag, sect_data, pSection.align());
  612. return size;
  613. }