reloc.cc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. // reloc.cc -- relocate input files for gold.
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <algorithm>
  19. #include "workqueue.h"
  20. #include "layout.h"
  21. #include "symtab.h"
  22. #include "output.h"
  23. #include "merge.h"
  24. #include "object.h"
  25. #include "target-reloc.h"
  26. #include "reloc.h"
  27. #include "icf.h"
  28. #include "compressed_output.h"
  29. #include "incremental.h"
  30. namespace gold
  31. {
  32. // Read_relocs methods.
  33. // These tasks just read the relocation information from the file.
  34. // After reading it, the start another task to process the
  35. // information. These tasks requires access to the file.
  36. Task_token*
  37. Read_relocs::is_runnable()
  38. {
  39. return this->object_->is_locked() ? this->object_->token() : NULL;
  40. }
  41. // Lock the file.
  42. void
  43. Read_relocs::locks(Task_locker* tl)
  44. {
  45. Task_token* token = this->object_->token();
  46. if (token != NULL)
  47. tl->add(this, token);
  48. }
  49. // Read the relocations and then start a Scan_relocs_task.
  50. void
  51. Read_relocs::run(Workqueue* workqueue)
  52. {
  53. Read_relocs_data* rd = new Read_relocs_data;
  54. this->object_->read_relocs(rd);
  55. this->object_->set_relocs_data(rd);
  56. this->object_->release();
  57. // If garbage collection or identical comdat folding is desired, we
  58. // process the relocs first before scanning them. Scanning of relocs is
  59. // done only after garbage or identical sections is identified.
  60. if (parameters->options().gc_sections()
  61. || parameters->options().icf_enabled())
  62. {
  63. workqueue->queue_next(new Gc_process_relocs(this->symtab_,
  64. this->layout_,
  65. this->object_, rd,
  66. this->this_blocker_,
  67. this->next_blocker_));
  68. }
  69. else
  70. {
  71. workqueue->queue_next(new Scan_relocs(this->symtab_, this->layout_,
  72. this->object_, rd,
  73. this->this_blocker_,
  74. this->next_blocker_));
  75. }
  76. }
  77. // Return a debugging name for the task.
  78. std::string
  79. Read_relocs::get_name() const
  80. {
  81. return "Read_relocs " + this->object_->name();
  82. }
  83. // Gc_process_relocs methods.
  84. Gc_process_relocs::~Gc_process_relocs()
  85. {
  86. if (this->this_blocker_ != NULL)
  87. delete this->this_blocker_;
  88. }
  89. // These tasks process the relocations read by Read_relocs and
  90. // determine which sections are referenced and which are garbage.
  91. // This task is done only when --gc-sections is used. This is blocked
  92. // by THIS_BLOCKER_. It unblocks NEXT_BLOCKER_.
  93. Task_token*
  94. Gc_process_relocs::is_runnable()
  95. {
  96. if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
  97. return this->this_blocker_;
  98. if (this->object_->is_locked())
  99. return this->object_->token();
  100. return NULL;
  101. }
  102. void
  103. Gc_process_relocs::locks(Task_locker* tl)
  104. {
  105. tl->add(this, this->object_->token());
  106. tl->add(this, this->next_blocker_);
  107. }
  108. void
  109. Gc_process_relocs::run(Workqueue*)
  110. {
  111. this->object_->gc_process_relocs(this->symtab_, this->layout_, this->rd_);
  112. this->object_->release();
  113. }
  114. // Return a debugging name for the task.
  115. std::string
  116. Gc_process_relocs::get_name() const
  117. {
  118. return "Gc_process_relocs " + this->object_->name();
  119. }
  120. // Scan_relocs methods.
  121. Scan_relocs::~Scan_relocs()
  122. {
  123. if (this->this_blocker_ != NULL)
  124. delete this->this_blocker_;
  125. }
  126. // These tasks scan the relocations read by Read_relocs and mark up
  127. // the symbol table to indicate which relocations are required. We
  128. // use a lock on the symbol table to keep them from interfering with
  129. // each other.
  130. Task_token*
  131. Scan_relocs::is_runnable()
  132. {
  133. if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
  134. return this->this_blocker_;
  135. if (this->object_->is_locked())
  136. return this->object_->token();
  137. return NULL;
  138. }
  139. // Return the locks we hold: one on the file, one on the symbol table
  140. // and one blocker.
  141. void
  142. Scan_relocs::locks(Task_locker* tl)
  143. {
  144. Task_token* token = this->object_->token();
  145. if (token != NULL)
  146. tl->add(this, token);
  147. tl->add(this, this->next_blocker_);
  148. }
  149. // Scan the relocs.
  150. void
  151. Scan_relocs::run(Workqueue*)
  152. {
  153. this->object_->scan_relocs(this->symtab_, this->layout_, this->rd_);
  154. delete this->rd_;
  155. this->rd_ = NULL;
  156. this->object_->release();
  157. }
  158. // Return a debugging name for the task.
  159. std::string
  160. Scan_relocs::get_name() const
  161. {
  162. return "Scan_relocs " + this->object_->name();
  163. }
  164. // Relocate_task methods.
  165. // We may have to wait for the output sections to be written.
  166. Task_token*
  167. Relocate_task::is_runnable()
  168. {
  169. if (this->object_->relocs_must_follow_section_writes()
  170. && this->output_sections_blocker_->is_blocked())
  171. return this->output_sections_blocker_;
  172. if (this->object_->is_locked())
  173. return this->object_->token();
  174. return NULL;
  175. }
  176. // We want to lock the file while we run. We want to unblock
  177. // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
  178. // INPUT_SECTIONS_BLOCKER may be NULL.
  179. void
  180. Relocate_task::locks(Task_locker* tl)
  181. {
  182. if (this->input_sections_blocker_ != NULL)
  183. tl->add(this, this->input_sections_blocker_);
  184. tl->add(this, this->final_blocker_);
  185. Task_token* token = this->object_->token();
  186. if (token != NULL)
  187. tl->add(this, token);
  188. }
  189. // Run the task.
  190. void
  191. Relocate_task::run(Workqueue*)
  192. {
  193. this->object_->relocate(this->symtab_, this->layout_, this->of_);
  194. // This is normally the last thing we will do with an object, so
  195. // uncache all views.
  196. this->object_->clear_view_cache_marks();
  197. this->object_->release();
  198. }
  199. // Return a debugging name for the task.
  200. std::string
  201. Relocate_task::get_name() const
  202. {
  203. return "Relocate_task " + this->object_->name();
  204. }
  205. // Read the relocs and local symbols from the object file and store
  206. // the information in RD.
  207. template<int size, bool big_endian>
  208. void
  209. Sized_relobj_file<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
  210. {
  211. rd->relocs.clear();
  212. unsigned int shnum = this->shnum();
  213. if (shnum == 0)
  214. return;
  215. rd->relocs.reserve(shnum / 2);
  216. const Output_sections& out_sections(this->output_sections());
  217. const std::vector<Address>& out_offsets(this->section_offsets());
  218. const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
  219. shnum * This::shdr_size,
  220. true, true);
  221. // Skip the first, dummy, section.
  222. const unsigned char* ps = pshdrs + This::shdr_size;
  223. for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
  224. {
  225. typename This::Shdr shdr(ps);
  226. unsigned int sh_type = shdr.get_sh_type();
  227. if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
  228. continue;
  229. unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
  230. if (shndx >= shnum)
  231. {
  232. this->error(_("relocation section %u has bad info %u"),
  233. i, shndx);
  234. continue;
  235. }
  236. Output_section* os = out_sections[shndx];
  237. if (os == NULL)
  238. continue;
  239. // We are scanning relocations in order to fill out the GOT and
  240. // PLT sections. Relocations for sections which are not
  241. // allocated (typically debugging sections) should not add new
  242. // GOT and PLT entries. So we skip them unless this is a
  243. // relocatable link or we need to emit relocations. FIXME: What
  244. // should we do if a linker script maps a section with SHF_ALLOC
  245. // clear to a section with SHF_ALLOC set?
  246. typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
  247. bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
  248. != 0);
  249. if (!is_section_allocated
  250. && !parameters->options().relocatable()
  251. && !parameters->options().emit_relocs()
  252. && !parameters->incremental())
  253. continue;
  254. if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
  255. {
  256. this->error(_("relocation section %u uses unexpected "
  257. "symbol table %u"),
  258. i, this->adjust_shndx(shdr.get_sh_link()));
  259. continue;
  260. }
  261. off_t sh_size = shdr.get_sh_size();
  262. if (sh_size == 0)
  263. continue;
  264. unsigned int reloc_size;
  265. if (sh_type == elfcpp::SHT_REL)
  266. reloc_size = elfcpp::Elf_sizes<size>::rel_size;
  267. else
  268. reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  269. if (reloc_size != shdr.get_sh_entsize())
  270. {
  271. this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
  272. i, static_cast<unsigned long>(shdr.get_sh_entsize()),
  273. reloc_size);
  274. continue;
  275. }
  276. size_t reloc_count = sh_size / reloc_size;
  277. if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
  278. {
  279. this->error(_("reloc section %u size %lu uneven"),
  280. i, static_cast<unsigned long>(sh_size));
  281. continue;
  282. }
  283. rd->relocs.push_back(Section_relocs());
  284. Section_relocs& sr(rd->relocs.back());
  285. sr.reloc_shndx = i;
  286. sr.data_shndx = shndx;
  287. sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
  288. true, true);
  289. sr.sh_type = sh_type;
  290. sr.reloc_count = reloc_count;
  291. sr.output_section = os;
  292. sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
  293. sr.is_data_section_allocated = is_section_allocated;
  294. }
  295. // Read the local symbols.
  296. gold_assert(this->symtab_shndx_ != -1U);
  297. if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
  298. rd->local_symbols = NULL;
  299. else
  300. {
  301. typename This::Shdr symtabshdr(pshdrs
  302. + this->symtab_shndx_ * This::shdr_size);
  303. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  304. const int sym_size = This::sym_size;
  305. const unsigned int loccount = this->local_symbol_count_;
  306. gold_assert(loccount == symtabshdr.get_sh_info());
  307. off_t locsize = loccount * sym_size;
  308. rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
  309. locsize, true, true);
  310. }
  311. }
  312. // Process the relocs to generate mappings from source sections to referenced
  313. // sections. This is used during garbage collection to determine garbage
  314. // sections.
  315. template<int size, bool big_endian>
  316. void
  317. Sized_relobj_file<size, big_endian>::do_gc_process_relocs(Symbol_table* symtab,
  318. Layout* layout,
  319. Read_relocs_data* rd)
  320. {
  321. Sized_target<size, big_endian>* target =
  322. parameters->sized_target<size, big_endian>();
  323. const unsigned char* local_symbols;
  324. if (rd->local_symbols == NULL)
  325. local_symbols = NULL;
  326. else
  327. local_symbols = rd->local_symbols->data();
  328. for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
  329. p != rd->relocs.end();
  330. ++p)
  331. {
  332. if (!parameters->options().relocatable())
  333. {
  334. // As noted above, when not generating an object file, we
  335. // only scan allocated sections. We may see a non-allocated
  336. // section here if we are emitting relocs.
  337. if (p->is_data_section_allocated)
  338. target->gc_process_relocs(symtab, layout, this,
  339. p->data_shndx, p->sh_type,
  340. p->contents->data(), p->reloc_count,
  341. p->output_section,
  342. p->needs_special_offset_handling,
  343. this->local_symbol_count_,
  344. local_symbols);
  345. }
  346. }
  347. }
  348. // Scan the relocs and adjust the symbol table. This looks for
  349. // relocations which require GOT/PLT/COPY relocations.
  350. template<int size, bool big_endian>
  351. void
  352. Sized_relobj_file<size, big_endian>::do_scan_relocs(Symbol_table* symtab,
  353. Layout* layout,
  354. Read_relocs_data* rd)
  355. {
  356. Sized_target<size, big_endian>* target =
  357. parameters->sized_target<size, big_endian>();
  358. const unsigned char* local_symbols;
  359. if (rd->local_symbols == NULL)
  360. local_symbols = NULL;
  361. else
  362. local_symbols = rd->local_symbols->data();
  363. // For incremental links, allocate the counters for incremental relocations.
  364. if (layout->incremental_inputs() != NULL)
  365. this->allocate_incremental_reloc_counts();
  366. for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
  367. p != rd->relocs.end();
  368. ++p)
  369. {
  370. // When garbage collection is on, unreferenced sections are not included
  371. // in the link that would have been included normally. This is known only
  372. // after Read_relocs hence this check has to be done again.
  373. if (parameters->options().gc_sections()
  374. || parameters->options().icf_enabled())
  375. {
  376. if (p->output_section == NULL)
  377. continue;
  378. }
  379. if (!parameters->options().relocatable())
  380. {
  381. // As noted above, when not generating an object file, we
  382. // only scan allocated sections. We may see a non-allocated
  383. // section here if we are emitting relocs.
  384. if (p->is_data_section_allocated)
  385. target->scan_relocs(symtab, layout, this, p->data_shndx,
  386. p->sh_type, p->contents->data(),
  387. p->reloc_count, p->output_section,
  388. p->needs_special_offset_handling,
  389. this->local_symbol_count_,
  390. local_symbols);
  391. if (parameters->options().emit_relocs())
  392. this->emit_relocs_scan(symtab, layout, local_symbols, p);
  393. if (layout->incremental_inputs() != NULL)
  394. this->incremental_relocs_scan(p);
  395. }
  396. else
  397. {
  398. Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
  399. gold_assert(rr != NULL);
  400. rr->set_reloc_count(p->reloc_count);
  401. target->scan_relocatable_relocs(symtab, layout, this,
  402. p->data_shndx, p->sh_type,
  403. p->contents->data(),
  404. p->reloc_count,
  405. p->output_section,
  406. p->needs_special_offset_handling,
  407. this->local_symbol_count_,
  408. local_symbols,
  409. rr);
  410. }
  411. delete p->contents;
  412. p->contents = NULL;
  413. }
  414. // For incremental links, finalize the allocation of relocations.
  415. if (layout->incremental_inputs() != NULL)
  416. this->finalize_incremental_relocs(layout, true);
  417. if (rd->local_symbols != NULL)
  418. {
  419. delete rd->local_symbols;
  420. rd->local_symbols = NULL;
  421. }
  422. }
  423. // Scan the input relocations for --emit-relocs.
  424. template<int size, bool big_endian>
  425. void
  426. Sized_relobj_file<size, big_endian>::emit_relocs_scan(
  427. Symbol_table* symtab,
  428. Layout* layout,
  429. const unsigned char* plocal_syms,
  430. const Read_relocs_data::Relocs_list::iterator& p)
  431. {
  432. Sized_target<size, big_endian>* target =
  433. parameters->sized_target<size, big_endian>();
  434. Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
  435. gold_assert(rr != NULL);
  436. rr->set_reloc_count(p->reloc_count);
  437. target->emit_relocs_scan(
  438. symtab,
  439. layout,
  440. this,
  441. p->data_shndx,
  442. p->sh_type,
  443. p->contents->data(),
  444. p->reloc_count,
  445. p->output_section,
  446. p->needs_special_offset_handling,
  447. this->local_symbol_count_,
  448. plocal_syms,
  449. rr);
  450. }
  451. // Scan the input relocations for --incremental.
  452. template<int size, bool big_endian>
  453. void
  454. Sized_relobj_file<size, big_endian>::incremental_relocs_scan(
  455. const Read_relocs_data::Relocs_list::iterator& p)
  456. {
  457. if (p->sh_type == elfcpp::SHT_REL)
  458. this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p);
  459. else
  460. {
  461. gold_assert(p->sh_type == elfcpp::SHT_RELA);
  462. this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p);
  463. }
  464. }
  465. // Scan the input relocation for --incremental, templatized on the
  466. // type of the relocation section.
  467. template<int size, bool big_endian>
  468. template<int sh_type>
  469. void
  470. Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype(
  471. const Read_relocs_data::Relocs_list::iterator& p)
  472. {
  473. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  474. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  475. const unsigned char* prelocs = p->contents->data();
  476. size_t reloc_count = p->reloc_count;
  477. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  478. {
  479. Reltype reloc(prelocs);
  480. if (p->needs_special_offset_handling
  481. && !p->output_section->is_input_address_mapped(this, p->data_shndx,
  482. reloc.get_r_offset()))
  483. continue;
  484. // FIXME: Some targets have a non-standard r_info field.
  485. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  486. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  487. if (r_sym >= this->local_symbol_count_)
  488. this->count_incremental_reloc(r_sym - this->local_symbol_count_);
  489. }
  490. }
  491. // Relocate the input sections and write out the local symbols.
  492. template<int size, bool big_endian>
  493. void
  494. Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab,
  495. const Layout* layout,
  496. Output_file* of)
  497. {
  498. unsigned int shnum = this->shnum();
  499. // Read the section headers.
  500. const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
  501. shnum * This::shdr_size,
  502. true, true);
  503. Views views;
  504. views.resize(shnum);
  505. // Make two passes over the sections. The first one copies the
  506. // section data to the output file. The second one applies
  507. // relocations.
  508. this->write_sections(layout, pshdrs, of, &views);
  509. // To speed up relocations, we set up hash tables for fast lookup of
  510. // input offsets to output addresses.
  511. this->initialize_input_to_output_maps();
  512. // Make the views available through get_output_view() for the duration
  513. // of this routine. This RAII class will reset output_views_ to NULL
  514. // when the views go out of scope.
  515. struct Set_output_views
  516. {
  517. Set_output_views(const Views** ppviews, const Views* pviews)
  518. {
  519. ppviews_ = ppviews;
  520. *ppviews = pviews;
  521. }
  522. ~Set_output_views()
  523. { *ppviews_ = NULL; }
  524. const Views** ppviews_;
  525. };
  526. Set_output_views set_output_views(&this->output_views_, &views);
  527. // Apply relocations.
  528. this->relocate_sections(symtab, layout, pshdrs, of, &views);
  529. // After we've done the relocations, we release the hash tables,
  530. // since we no longer need them.
  531. this->free_input_to_output_maps();
  532. // Write out the accumulated views.
  533. for (unsigned int i = 1; i < shnum; ++i)
  534. {
  535. if (views[i].view != NULL)
  536. {
  537. if (views[i].is_ctors_reverse_view)
  538. this->reverse_words(views[i].view, views[i].view_size);
  539. if (!views[i].is_postprocessing_view)
  540. {
  541. if (views[i].is_input_output_view)
  542. of->write_input_output_view(views[i].offset,
  543. views[i].view_size,
  544. views[i].view);
  545. else
  546. of->write_output_view(views[i].offset, views[i].view_size,
  547. views[i].view);
  548. }
  549. }
  550. }
  551. // Write out the local symbols.
  552. this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
  553. layout->symtab_xindex(), layout->dynsym_xindex(),
  554. layout->symtab_section_offset());
  555. }
  556. // Sort a Read_multiple vector by file offset.
  557. struct Read_multiple_compare
  558. {
  559. inline bool
  560. operator()(const File_read::Read_multiple_entry& rme1,
  561. const File_read::Read_multiple_entry& rme2) const
  562. { return rme1.file_offset < rme2.file_offset; }
  563. };
  564. // Write section data to the output file. PSHDRS points to the
  565. // section headers. Record the views in *PVIEWS for use when
  566. // relocating.
  567. template<int size, bool big_endian>
  568. void
  569. Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout,
  570. const unsigned char* pshdrs,
  571. Output_file* of,
  572. Views* pviews)
  573. {
  574. unsigned int shnum = this->shnum();
  575. const Output_sections& out_sections(this->output_sections());
  576. const std::vector<Address>& out_offsets(this->section_offsets());
  577. File_read::Read_multiple rm;
  578. bool is_sorted = true;
  579. const unsigned char* p = pshdrs + This::shdr_size;
  580. for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
  581. {
  582. View_size* pvs = &(*pviews)[i];
  583. pvs->view = NULL;
  584. const Output_section* os = out_sections[i];
  585. if (os == NULL)
  586. continue;
  587. Address output_offset = out_offsets[i];
  588. typename This::Shdr shdr(p);
  589. if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
  590. continue;
  591. if ((parameters->options().relocatable()
  592. || parameters->options().emit_relocs())
  593. && (shdr.get_sh_type() == elfcpp::SHT_REL
  594. || shdr.get_sh_type() == elfcpp::SHT_RELA)
  595. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  596. {
  597. // This is a reloc section in a relocatable link or when
  598. // emitting relocs. We don't need to read the input file.
  599. // The size and file offset are stored in the
  600. // Relocatable_relocs structure.
  601. Relocatable_relocs* rr = this->relocatable_relocs(i);
  602. gold_assert(rr != NULL);
  603. Output_data* posd = rr->output_data();
  604. gold_assert(posd != NULL);
  605. pvs->offset = posd->offset();
  606. pvs->view_size = posd->data_size();
  607. pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
  608. pvs->address = posd->address();
  609. pvs->is_input_output_view = false;
  610. pvs->is_postprocessing_view = false;
  611. pvs->is_ctors_reverse_view = false;
  612. continue;
  613. }
  614. // In the normal case, this input section is simply mapped to
  615. // the output section at offset OUTPUT_OFFSET.
  616. // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
  617. // handled specially--e.g., a .eh_frame section. The relocation
  618. // routines need to check for each reloc where it should be
  619. // applied. For this case, we need an input/output view for the
  620. // entire contents of the section in the output file. We don't
  621. // want to copy the contents of the input section to the output
  622. // section; the output section contents were already written,
  623. // and we waited for them in Relocate_task::is_runnable because
  624. // relocs_must_follow_section_writes is set for the object.
  625. // Regardless of which of the above cases is true, we have to
  626. // check requires_postprocessing of the output section. If that
  627. // is false, then we work with views of the output file
  628. // directly. If it is true, then we work with a separate
  629. // buffer, and the output section is responsible for writing the
  630. // final data to the output file.
  631. off_t output_section_offset;
  632. Address output_section_size;
  633. if (!os->requires_postprocessing())
  634. {
  635. output_section_offset = os->offset();
  636. output_section_size = convert_types<Address, off_t>(os->data_size());
  637. }
  638. else
  639. {
  640. output_section_offset = 0;
  641. output_section_size =
  642. convert_types<Address, off_t>(os->postprocessing_buffer_size());
  643. }
  644. off_t view_start;
  645. section_size_type view_size;
  646. bool must_decompress = false;
  647. if (output_offset != invalid_address)
  648. {
  649. view_start = output_section_offset + output_offset;
  650. view_size = convert_to_section_size_type(shdr.get_sh_size());
  651. section_size_type uncompressed_size;
  652. if (this->section_is_compressed(i, &uncompressed_size))
  653. {
  654. view_size = uncompressed_size;
  655. must_decompress = true;
  656. }
  657. }
  658. else
  659. {
  660. view_start = output_section_offset;
  661. view_size = convert_to_section_size_type(output_section_size);
  662. }
  663. if (view_size == 0)
  664. continue;
  665. gold_assert(output_offset == invalid_address
  666. || output_offset + view_size <= output_section_size);
  667. unsigned char* view;
  668. if (os->requires_postprocessing())
  669. {
  670. unsigned char* buffer = os->postprocessing_buffer();
  671. view = buffer + view_start;
  672. if (output_offset != invalid_address && !must_decompress)
  673. {
  674. off_t sh_offset = shdr.get_sh_offset();
  675. if (!rm.empty() && rm.back().file_offset > sh_offset)
  676. is_sorted = false;
  677. rm.push_back(File_read::Read_multiple_entry(sh_offset,
  678. view_size, view));
  679. }
  680. }
  681. else
  682. {
  683. if (output_offset == invalid_address)
  684. view = of->get_input_output_view(view_start, view_size);
  685. else
  686. {
  687. view = of->get_output_view(view_start, view_size);
  688. if (!must_decompress)
  689. {
  690. off_t sh_offset = shdr.get_sh_offset();
  691. if (!rm.empty() && rm.back().file_offset > sh_offset)
  692. is_sorted = false;
  693. rm.push_back(File_read::Read_multiple_entry(sh_offset,
  694. view_size, view));
  695. }
  696. }
  697. }
  698. if (must_decompress)
  699. {
  700. // Read and decompress the section.
  701. section_size_type len;
  702. const unsigned char* p = this->section_contents(i, &len, false);
  703. if (!decompress_input_section(p, len, view, view_size,
  704. size, big_endian,
  705. shdr.get_sh_flags()))
  706. this->error(_("could not decompress section %s"),
  707. this->section_name(i).c_str());
  708. }
  709. pvs->view = view;
  710. pvs->address = os->address();
  711. if (output_offset != invalid_address)
  712. pvs->address += output_offset;
  713. pvs->offset = view_start;
  714. pvs->view_size = view_size;
  715. pvs->is_input_output_view = output_offset == invalid_address;
  716. pvs->is_postprocessing_view = os->requires_postprocessing();
  717. pvs->is_ctors_reverse_view =
  718. (!parameters->options().relocatable()
  719. && view_size > size / 8
  720. && (strcmp(os->name(), ".init_array") == 0
  721. || strcmp(os->name(), ".fini_array") == 0)
  722. && layout->is_ctors_in_init_array(this, i));
  723. }
  724. // Actually read the data.
  725. if (!rm.empty())
  726. {
  727. if (!is_sorted)
  728. std::sort(rm.begin(), rm.end(), Read_multiple_compare());
  729. this->read_multiple(rm);
  730. }
  731. }
  732. // Relocate section data. VIEWS points to the section data as views
  733. // in the output file.
  734. template<int size, bool big_endian>
  735. void
  736. Sized_relobj_file<size, big_endian>::do_relocate_sections(
  737. const Symbol_table* symtab,
  738. const Layout* layout,
  739. const unsigned char* pshdrs,
  740. Output_file* of,
  741. Views* pviews)
  742. {
  743. this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
  744. 1, this->shnum() - 1);
  745. }
  746. // Relocate section data for the range of sections START_SHNDX through
  747. // END_SHNDX.
  748. template<int size, bool big_endian>
  749. void
  750. Sized_relobj_file<size, big_endian>::relocate_section_range(
  751. const Symbol_table* symtab,
  752. const Layout* layout,
  753. const unsigned char* pshdrs,
  754. Output_file* of,
  755. Views* pviews,
  756. unsigned int start_shndx,
  757. unsigned int end_shndx)
  758. {
  759. gold_assert(start_shndx >= 1);
  760. gold_assert(end_shndx < this->shnum());
  761. if (end_shndx < start_shndx)
  762. return;
  763. Sized_target<size, big_endian>* target =
  764. parameters->sized_target<size, big_endian>();
  765. const Output_sections& out_sections(this->output_sections());
  766. const std::vector<Address>& out_offsets(this->section_offsets());
  767. Relocate_info<size, big_endian> relinfo;
  768. relinfo.symtab = symtab;
  769. relinfo.layout = layout;
  770. relinfo.object = this;
  771. const unsigned char* p = pshdrs + start_shndx * This::shdr_size;
  772. for (unsigned int i = start_shndx; i <= end_shndx; ++i, p += This::shdr_size)
  773. {
  774. typename This::Shdr shdr(p);
  775. unsigned int sh_type = shdr.get_sh_type();
  776. if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
  777. continue;
  778. off_t sh_size = shdr.get_sh_size();
  779. if (sh_size == 0)
  780. continue;
  781. unsigned int index = this->adjust_shndx(shdr.get_sh_info());
  782. if (index >= this->shnum())
  783. {
  784. this->error(_("relocation section %u has bad info %u"),
  785. i, index);
  786. continue;
  787. }
  788. Output_section* os = out_sections[index];
  789. if (os == NULL)
  790. {
  791. // This relocation section is against a section which we
  792. // discarded.
  793. continue;
  794. }
  795. Address output_offset = out_offsets[index];
  796. gold_assert((*pviews)[index].view != NULL);
  797. if (parameters->options().relocatable())
  798. gold_assert((*pviews)[i].view != NULL);
  799. if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
  800. {
  801. gold_error(_("relocation section %u uses unexpected "
  802. "symbol table %u"),
  803. i, this->adjust_shndx(shdr.get_sh_link()));
  804. continue;
  805. }
  806. const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
  807. sh_size, true, false);
  808. unsigned int reloc_size;
  809. if (sh_type == elfcpp::SHT_REL)
  810. reloc_size = elfcpp::Elf_sizes<size>::rel_size;
  811. else
  812. reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  813. if (reloc_size != shdr.get_sh_entsize())
  814. {
  815. gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
  816. i, static_cast<unsigned long>(shdr.get_sh_entsize()),
  817. reloc_size);
  818. continue;
  819. }
  820. size_t reloc_count = sh_size / reloc_size;
  821. if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
  822. {
  823. gold_error(_("reloc section %u size %lu uneven"),
  824. i, static_cast<unsigned long>(sh_size));
  825. continue;
  826. }
  827. gold_assert(output_offset != invalid_address
  828. || this->relocs_must_follow_section_writes());
  829. relinfo.reloc_shndx = i;
  830. relinfo.reloc_shdr = p;
  831. relinfo.data_shndx = index;
  832. relinfo.data_shdr = pshdrs + index * This::shdr_size;
  833. unsigned char* view = (*pviews)[index].view;
  834. Address address = (*pviews)[index].address;
  835. section_size_type view_size = (*pviews)[index].view_size;
  836. Reloc_symbol_changes* reloc_map = NULL;
  837. if (this->uses_split_stack() && output_offset != invalid_address)
  838. {
  839. typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
  840. if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
  841. this->split_stack_adjust(symtab, pshdrs, sh_type, index,
  842. prelocs, reloc_count, view, view_size,
  843. &reloc_map, target);
  844. }
  845. Relocatable_relocs* rr = NULL;
  846. if (parameters->options().emit_relocs()
  847. || parameters->options().relocatable())
  848. rr = this->relocatable_relocs(i);
  849. relinfo.rr = rr;
  850. if (!parameters->options().relocatable())
  851. {
  852. target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
  853. output_offset == invalid_address,
  854. view, address, view_size, reloc_map);
  855. if (parameters->options().emit_relocs())
  856. target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
  857. os, output_offset,
  858. view, address, view_size,
  859. (*pviews)[i].view,
  860. (*pviews)[i].view_size);
  861. if (parameters->incremental())
  862. this->incremental_relocs_write(&relinfo, sh_type, prelocs,
  863. reloc_count, os, output_offset, of);
  864. }
  865. else
  866. target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
  867. os, output_offset,
  868. view, address, view_size,
  869. (*pviews)[i].view,
  870. (*pviews)[i].view_size);
  871. }
  872. }
  873. // Return the output view for section SHNDX.
  874. template<int size, bool big_endian>
  875. unsigned char*
  876. Sized_relobj_file<size, big_endian>::do_get_output_view(
  877. unsigned int shndx,
  878. section_size_type* plen) const
  879. {
  880. gold_assert(this->output_views_ != NULL);
  881. gold_assert(shndx < this->output_views_->size());
  882. const View_size& v = (*this->output_views_)[shndx];
  883. *plen = v.view_size;
  884. return v.view;
  885. }
  886. // Write the incremental relocs.
  887. template<int size, bool big_endian>
  888. void
  889. Sized_relobj_file<size, big_endian>::incremental_relocs_write(
  890. const Relocate_info<size, big_endian>* relinfo,
  891. unsigned int sh_type,
  892. const unsigned char* prelocs,
  893. size_t reloc_count,
  894. Output_section* output_section,
  895. Address output_offset,
  896. Output_file* of)
  897. {
  898. if (sh_type == elfcpp::SHT_REL)
  899. this->incremental_relocs_write_reltype<elfcpp::SHT_REL>(
  900. relinfo,
  901. prelocs,
  902. reloc_count,
  903. output_section,
  904. output_offset,
  905. of);
  906. else
  907. {
  908. gold_assert(sh_type == elfcpp::SHT_RELA);
  909. this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>(
  910. relinfo,
  911. prelocs,
  912. reloc_count,
  913. output_section,
  914. output_offset,
  915. of);
  916. }
  917. }
  918. // Write the incremental relocs, templatized on the type of the
  919. // relocation section.
  920. template<int size, bool big_endian>
  921. template<int sh_type>
  922. void
  923. Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype(
  924. const Relocate_info<size, big_endian>* relinfo,
  925. const unsigned char* prelocs,
  926. size_t reloc_count,
  927. Output_section* output_section,
  928. Address output_offset,
  929. Output_file* of)
  930. {
  931. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
  932. const unsigned int reloc_size =
  933. Reloc_types<sh_type, size, big_endian>::reloc_size;
  934. const unsigned int sizeof_addr = size / 8;
  935. const unsigned int incr_reloc_size =
  936. Incremental_relocs_reader<size, big_endian>::reloc_size;
  937. unsigned int out_shndx = output_section->out_shndx();
  938. // Get a view for the .gnu_incremental_relocs section.
  939. Incremental_inputs* inputs = relinfo->layout->incremental_inputs();
  940. gold_assert(inputs != NULL);
  941. const off_t relocs_off = inputs->relocs_section()->offset();
  942. const off_t relocs_size = inputs->relocs_section()->data_size();
  943. unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
  944. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  945. {
  946. Reloc reloc(prelocs);
  947. // FIXME: Some targets have a non-standard r_info field.
  948. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  949. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  950. const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  951. if (r_sym < this->local_symbol_count_)
  952. continue;
  953. // Get the new offset--the location in the output section where
  954. // this relocation should be applied.
  955. Address offset = reloc.get_r_offset();
  956. if (output_offset != invalid_address)
  957. offset += output_offset;
  958. else
  959. {
  960. section_offset_type sot_offset =
  961. convert_types<section_offset_type, Address>(offset);
  962. section_offset_type new_sot_offset =
  963. output_section->output_offset(relinfo->object,
  964. relinfo->data_shndx,
  965. sot_offset);
  966. gold_assert(new_sot_offset != -1);
  967. offset += new_sot_offset;
  968. }
  969. // Get the addend.
  970. typename elfcpp::Elf_types<size>::Elf_Swxword addend;
  971. if (sh_type == elfcpp::SHT_RELA)
  972. addend =
  973. Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc);
  974. else
  975. {
  976. // FIXME: Get the addend for SHT_REL.
  977. addend = 0;
  978. }
  979. // Get the index of the output relocation.
  980. unsigned int reloc_index =
  981. this->next_incremental_reloc_index(r_sym - this->local_symbol_count_);
  982. // Write the relocation.
  983. unsigned char* pov = view + reloc_index * incr_reloc_size;
  984. elfcpp::Swap<32, big_endian>::writeval(pov, r_type);
  985. elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx);
  986. elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset);
  987. elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend);
  988. of->write_output_view(pov - view, incr_reloc_size, view);
  989. }
  990. }
  991. // Create merge hash tables for the local symbols. These are used to
  992. // speed up relocations.
  993. template<int size, bool big_endian>
  994. void
  995. Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps()
  996. {
  997. const unsigned int loccount = this->local_symbol_count_;
  998. for (unsigned int i = 1; i < loccount; ++i)
  999. {
  1000. Symbol_value<size>& lv(this->local_values_[i]);
  1001. lv.initialize_input_to_output_map(this);
  1002. }
  1003. }
  1004. // Free merge hash tables for the local symbols.
  1005. template<int size, bool big_endian>
  1006. void
  1007. Sized_relobj_file<size, big_endian>::free_input_to_output_maps()
  1008. {
  1009. const unsigned int loccount = this->local_symbol_count_;
  1010. for (unsigned int i = 1; i < loccount; ++i)
  1011. {
  1012. Symbol_value<size>& lv(this->local_values_[i]);
  1013. lv.free_input_to_output_map();
  1014. }
  1015. }
  1016. // If an object was compiled with -fsplit-stack, this is called to
  1017. // check whether any relocations refer to functions defined in objects
  1018. // which were not compiled with -fsplit-stack. If they were, then we
  1019. // need to apply some target-specific adjustments to request
  1020. // additional stack space.
  1021. template<int size, bool big_endian>
  1022. void
  1023. Sized_relobj_file<size, big_endian>::split_stack_adjust(
  1024. const Symbol_table* symtab,
  1025. const unsigned char* pshdrs,
  1026. unsigned int sh_type,
  1027. unsigned int shndx,
  1028. const unsigned char* prelocs,
  1029. size_t reloc_count,
  1030. unsigned char* view,
  1031. section_size_type view_size,
  1032. Reloc_symbol_changes** reloc_map,
  1033. const Sized_target<size, big_endian>* target)
  1034. {
  1035. if (sh_type == elfcpp::SHT_REL)
  1036. this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
  1037. prelocs, reloc_count,
  1038. view, view_size,
  1039. reloc_map, target);
  1040. else
  1041. {
  1042. gold_assert(sh_type == elfcpp::SHT_RELA);
  1043. this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
  1044. prelocs, reloc_count,
  1045. view, view_size,
  1046. reloc_map, target);
  1047. }
  1048. }
  1049. // Adjust for -fsplit-stack, templatized on the type of the relocation
  1050. // section.
  1051. template<int size, bool big_endian>
  1052. template<int sh_type>
  1053. void
  1054. Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype(
  1055. const Symbol_table* symtab,
  1056. const unsigned char* pshdrs,
  1057. unsigned int shndx,
  1058. const unsigned char* prelocs,
  1059. size_t reloc_count,
  1060. unsigned char* view,
  1061. section_size_type view_size,
  1062. Reloc_symbol_changes** reloc_map,
  1063. const Sized_target<size, big_endian>* target)
  1064. {
  1065. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  1066. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  1067. size_t local_count = this->local_symbol_count();
  1068. std::vector<section_offset_type> non_split_refs;
  1069. const unsigned char* pr = prelocs;
  1070. for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
  1071. {
  1072. // Some supported targets have a non-standard r_info field.
  1073. // If this call is too slow, we can move this routine to
  1074. // target-reloc.h and templatize it on Classify_reloc.
  1075. unsigned int r_sym = target->get_r_sym(pr);
  1076. if (r_sym < local_count)
  1077. continue;
  1078. const Symbol* gsym = this->global_symbol(r_sym);
  1079. gold_assert(gsym != NULL);
  1080. if (gsym->is_forwarder())
  1081. gsym = symtab->resolve_forwards(gsym);
  1082. // See if this relocation refers to a function defined in an
  1083. // object compiled without -fsplit-stack. Note that we don't
  1084. // care about the type of relocation--this means that in some
  1085. // cases we will ask for a large stack unnecessarily, but this
  1086. // is not fatal. FIXME: Some targets have symbols which are
  1087. // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
  1088. if (!gsym->is_undefined()
  1089. && gsym->source() == Symbol::FROM_OBJECT
  1090. && !gsym->object()->uses_split_stack())
  1091. {
  1092. if (parameters->target().is_call_to_non_split(gsym, pr, view,
  1093. view_size))
  1094. {
  1095. Reltype reloc(pr);
  1096. section_offset_type offset =
  1097. convert_to_section_size_type(reloc.get_r_offset());
  1098. non_split_refs.push_back(offset);
  1099. }
  1100. }
  1101. }
  1102. if (non_split_refs.empty())
  1103. return;
  1104. // At this point, every entry in NON_SPLIT_REFS indicates a
  1105. // relocation which refers to a function in an object compiled
  1106. // without -fsplit-stack. We now have to convert that list into a
  1107. // set of offsets to functions. First, we find all the functions.
  1108. Function_offsets function_offsets;
  1109. this->find_functions(pshdrs, shndx, &function_offsets);
  1110. if (function_offsets.empty())
  1111. return;
  1112. // Now get a list of the function with references to non split-stack
  1113. // code.
  1114. Function_offsets calls_non_split;
  1115. for (std::vector<section_offset_type>::const_iterator p
  1116. = non_split_refs.begin();
  1117. p != non_split_refs.end();
  1118. ++p)
  1119. {
  1120. Function_offsets::const_iterator low = function_offsets.lower_bound(*p);
  1121. if (low == function_offsets.end())
  1122. --low;
  1123. else if (low->first == *p)
  1124. ;
  1125. else if (low == function_offsets.begin())
  1126. continue;
  1127. else
  1128. --low;
  1129. calls_non_split.insert(*low);
  1130. }
  1131. if (calls_non_split.empty())
  1132. return;
  1133. // Now we have a set of functions to adjust. The adjustments are
  1134. // target specific. Besides changing the output section view
  1135. // however, it likes, the target may request a relocation change
  1136. // from one global symbol name to another.
  1137. for (Function_offsets::const_iterator p = calls_non_split.begin();
  1138. p != calls_non_split.end();
  1139. ++p)
  1140. {
  1141. std::string from;
  1142. std::string to;
  1143. parameters->target().calls_non_split(this, shndx, p->first, p->second,
  1144. prelocs, reloc_count,
  1145. view, view_size, &from, &to);
  1146. if (!from.empty())
  1147. {
  1148. gold_assert(!to.empty());
  1149. Symbol* tosym = NULL;
  1150. // Find relocations in the relevant function which are for
  1151. // FROM.
  1152. pr = prelocs;
  1153. for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
  1154. {
  1155. Reltype reloc(pr);
  1156. unsigned int r_sym = target->get_r_sym(pr);
  1157. if (r_sym < local_count)
  1158. continue;
  1159. section_offset_type offset =
  1160. convert_to_section_size_type(reloc.get_r_offset());
  1161. if (offset < p->first
  1162. || (offset
  1163. >= (p->first
  1164. + static_cast<section_offset_type>(p->second))))
  1165. continue;
  1166. const Symbol* gsym = this->global_symbol(r_sym);
  1167. if (from == gsym->name())
  1168. {
  1169. if (tosym == NULL)
  1170. {
  1171. tosym = symtab->lookup(to.c_str());
  1172. if (tosym == NULL)
  1173. {
  1174. this->error(_("could not convert call "
  1175. "to '%s' to '%s'"),
  1176. from.c_str(), to.c_str());
  1177. break;
  1178. }
  1179. }
  1180. if (*reloc_map == NULL)
  1181. *reloc_map = new Reloc_symbol_changes(reloc_count);
  1182. (*reloc_map)->set(i, tosym);
  1183. }
  1184. }
  1185. }
  1186. }
  1187. }
  1188. // Find all the function in this object defined in section SHNDX.
  1189. // Store their offsets in the section in FUNCTION_OFFSETS.
  1190. template<int size, bool big_endian>
  1191. void
  1192. Sized_relobj_file<size, big_endian>::find_functions(
  1193. const unsigned char* pshdrs,
  1194. unsigned int shndx,
  1195. Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets)
  1196. {
  1197. // We need to read the symbols to find the functions. If we wanted
  1198. // to, we could cache reading the symbols across all sections in the
  1199. // object.
  1200. const unsigned int symtab_shndx = this->symtab_shndx_;
  1201. typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
  1202. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  1203. typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
  1204. symtabshdr.get_sh_size();
  1205. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  1206. sh_size, true, true);
  1207. const int sym_size = This::sym_size;
  1208. const unsigned int symcount = sh_size / sym_size;
  1209. for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
  1210. {
  1211. typename elfcpp::Sym<size, big_endian> isym(psyms);
  1212. // FIXME: Some targets can have functions which do not have type
  1213. // STT_FUNC, e.g., STT_ARM_TFUNC.
  1214. if (isym.get_st_type() != elfcpp::STT_FUNC
  1215. || isym.get_st_size() == 0)
  1216. continue;
  1217. bool is_ordinary;
  1218. Symbol_location loc;
  1219. loc.shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
  1220. &is_ordinary);
  1221. if (!is_ordinary)
  1222. continue;
  1223. loc.object = this;
  1224. loc.offset = isym.get_st_value();
  1225. parameters->target().function_location(&loc);
  1226. if (loc.shndx != shndx)
  1227. continue;
  1228. section_offset_type value =
  1229. convert_to_section_size_type(loc.offset);
  1230. section_size_type fnsize =
  1231. convert_to_section_size_type(isym.get_st_size());
  1232. (*function_offsets)[value] = fnsize;
  1233. }
  1234. }
  1235. // Reverse the words in a section. Used for .ctors sections mapped to
  1236. // .init_array sections. See ctors_sections_in_init_array in
  1237. // layout.cc.
  1238. template<int size, bool big_endian>
  1239. void
  1240. Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view,
  1241. section_size_type view_size)
  1242. {
  1243. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  1244. Valtype* vview = reinterpret_cast<Valtype*>(view);
  1245. section_size_type vview_size = view_size / (size / 8);
  1246. for (section_size_type i = 0; i < vview_size / 2; ++i)
  1247. {
  1248. Valtype tmp = vview[i];
  1249. vview[i] = vview[vview_size - 1 - i];
  1250. vview[vview_size - 1 - i] = tmp;
  1251. }
  1252. }
  1253. // Class Merged_symbol_value.
  1254. template<int size>
  1255. void
  1256. Merged_symbol_value<size>::initialize_input_to_output_map(
  1257. const Relobj* object,
  1258. unsigned int input_shndx)
  1259. {
  1260. object->initialize_input_to_output_map<size>(input_shndx,
  1261. this->output_start_address_,
  1262. &this->output_addresses_);
  1263. }
  1264. // Get the output value corresponding to an input offset if we
  1265. // couldn't find it in the hash table.
  1266. template<int size>
  1267. typename elfcpp::Elf_types<size>::Elf_Addr
  1268. Merged_symbol_value<size>::value_from_output_section(
  1269. const Relobj* object,
  1270. unsigned int input_shndx,
  1271. typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
  1272. {
  1273. section_offset_type output_offset;
  1274. bool found = object->merge_output_offset(input_shndx, input_offset,
  1275. &output_offset);
  1276. // If this assertion fails, it means that some relocation was
  1277. // against a portion of an input merge section which we didn't map
  1278. // to the output file and we didn't explicitly discard. We should
  1279. // always map all portions of input merge sections.
  1280. gold_assert(found);
  1281. if (output_offset == -1)
  1282. return 0;
  1283. else
  1284. return this->output_start_address_ + output_offset;
  1285. }
  1286. // Track_relocs methods.
  1287. // Initialize the class to track the relocs. This gets the object,
  1288. // the reloc section index, and the type of the relocs. This returns
  1289. // false if something goes wrong.
  1290. template<int size, bool big_endian>
  1291. bool
  1292. Track_relocs<size, big_endian>::initialize(
  1293. Object* object,
  1294. unsigned int reloc_shndx,
  1295. unsigned int reloc_type)
  1296. {
  1297. // If RELOC_SHNDX is -1U, it means there is more than one reloc
  1298. // section for the .eh_frame section. We can't handle that case.
  1299. if (reloc_shndx == -1U)
  1300. return false;
  1301. // If RELOC_SHNDX is 0, there is no reloc section.
  1302. if (reloc_shndx == 0)
  1303. return true;
  1304. // Get the contents of the reloc section.
  1305. this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
  1306. if (reloc_type == elfcpp::SHT_REL)
  1307. this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
  1308. else if (reloc_type == elfcpp::SHT_RELA)
  1309. this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
  1310. else
  1311. gold_unreachable();
  1312. if (this->len_ % this->reloc_size_ != 0)
  1313. {
  1314. object->error(_("reloc section size %zu is not a multiple of "
  1315. "reloc size %d\n"),
  1316. static_cast<size_t>(this->len_),
  1317. this->reloc_size_);
  1318. return false;
  1319. }
  1320. return true;
  1321. }
  1322. // Return the offset of the next reloc, or -1 if there isn't one.
  1323. template<int size, bool big_endian>
  1324. off_t
  1325. Track_relocs<size, big_endian>::next_offset() const
  1326. {
  1327. if (this->pos_ >= this->len_)
  1328. return -1;
  1329. // Rel and Rela start out the same, so we can always use Rel to find
  1330. // the r_offset value.
  1331. elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
  1332. return rel.get_r_offset();
  1333. }
  1334. // Return the index of the symbol referenced by the next reloc, or -1U
  1335. // if there aren't any more relocs.
  1336. template<int size, bool big_endian>
  1337. unsigned int
  1338. Track_relocs<size, big_endian>::next_symndx() const
  1339. {
  1340. if (this->pos_ >= this->len_)
  1341. return -1U;
  1342. Sized_target<size, big_endian>* target
  1343. = parameters->sized_target<size, big_endian>();
  1344. return target->get_r_sym(this->prelocs_ + this->pos_);
  1345. }
  1346. // Return the addend of the next reloc, or 0 if there isn't one.
  1347. template<int size, bool big_endian>
  1348. uint64_t
  1349. Track_relocs<size, big_endian>::next_addend() const
  1350. {
  1351. if (this->pos_ >= this->len_)
  1352. return 0;
  1353. if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size)
  1354. return 0;
  1355. elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_);
  1356. return rela.get_r_addend();
  1357. }
  1358. // Advance to the next reloc whose r_offset is greater than or equal
  1359. // to OFFSET. Return the number of relocs we skip.
  1360. template<int size, bool big_endian>
  1361. int
  1362. Track_relocs<size, big_endian>::advance(off_t offset)
  1363. {
  1364. int ret = 0;
  1365. while (this->pos_ < this->len_)
  1366. {
  1367. // Rel and Rela start out the same, so we can always use Rel to
  1368. // find the r_offset value.
  1369. elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
  1370. if (static_cast<off_t>(rel.get_r_offset()) >= offset)
  1371. break;
  1372. // Skip R_*_NONE relocation entries with r_sym of zero
  1373. // without counting.
  1374. if (rel.get_r_info() != 0)
  1375. ++ret;
  1376. this->pos_ += this->reloc_size_;
  1377. }
  1378. return ret;
  1379. }
  1380. // Instantiate the templates we need.
  1381. #ifdef HAVE_TARGET_32_LITTLE
  1382. template
  1383. void
  1384. Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd);
  1385. #endif
  1386. #ifdef HAVE_TARGET_32_BIG
  1387. template
  1388. void
  1389. Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd);
  1390. #endif
  1391. #ifdef HAVE_TARGET_64_LITTLE
  1392. template
  1393. void
  1394. Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd);
  1395. #endif
  1396. #ifdef HAVE_TARGET_64_BIG
  1397. template
  1398. void
  1399. Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd);
  1400. #endif
  1401. #ifdef HAVE_TARGET_32_LITTLE
  1402. template
  1403. void
  1404. Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab,
  1405. Layout* layout,
  1406. Read_relocs_data* rd);
  1407. #endif
  1408. #ifdef HAVE_TARGET_32_BIG
  1409. template
  1410. void
  1411. Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab,
  1412. Layout* layout,
  1413. Read_relocs_data* rd);
  1414. #endif
  1415. #ifdef HAVE_TARGET_64_LITTLE
  1416. template
  1417. void
  1418. Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab,
  1419. Layout* layout,
  1420. Read_relocs_data* rd);
  1421. #endif
  1422. #ifdef HAVE_TARGET_64_BIG
  1423. template
  1424. void
  1425. Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab,
  1426. Layout* layout,
  1427. Read_relocs_data* rd);
  1428. #endif
  1429. #ifdef HAVE_TARGET_32_LITTLE
  1430. template
  1431. void
  1432. Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab,
  1433. Layout* layout,
  1434. Read_relocs_data* rd);
  1435. #endif
  1436. #ifdef HAVE_TARGET_32_BIG
  1437. template
  1438. void
  1439. Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab,
  1440. Layout* layout,
  1441. Read_relocs_data* rd);
  1442. #endif
  1443. #ifdef HAVE_TARGET_64_LITTLE
  1444. template
  1445. void
  1446. Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab,
  1447. Layout* layout,
  1448. Read_relocs_data* rd);
  1449. #endif
  1450. #ifdef HAVE_TARGET_64_BIG
  1451. template
  1452. void
  1453. Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab,
  1454. Layout* layout,
  1455. Read_relocs_data* rd);
  1456. #endif
  1457. #ifdef HAVE_TARGET_32_LITTLE
  1458. template
  1459. void
  1460. Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab,
  1461. const Layout* layout,
  1462. Output_file* of);
  1463. #endif
  1464. #ifdef HAVE_TARGET_32_BIG
  1465. template
  1466. void
  1467. Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab,
  1468. const Layout* layout,
  1469. Output_file* of);
  1470. #endif
  1471. #ifdef HAVE_TARGET_64_LITTLE
  1472. template
  1473. void
  1474. Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab,
  1475. const Layout* layout,
  1476. Output_file* of);
  1477. #endif
  1478. #ifdef HAVE_TARGET_64_BIG
  1479. template
  1480. void
  1481. Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab,
  1482. const Layout* layout,
  1483. Output_file* of);
  1484. #endif
  1485. #ifdef HAVE_TARGET_32_LITTLE
  1486. template
  1487. void
  1488. Sized_relobj_file<32, false>::do_relocate_sections(
  1489. const Symbol_table* symtab,
  1490. const Layout* layout,
  1491. const unsigned char* pshdrs,
  1492. Output_file* of,
  1493. Views* pviews);
  1494. template
  1495. void
  1496. Sized_relobj_file<32, false>::relocate_section_range(
  1497. const Symbol_table* symtab,
  1498. const Layout* layout,
  1499. const unsigned char* pshdrs,
  1500. Output_file* of,
  1501. Views* pviews,
  1502. unsigned int start_shndx,
  1503. unsigned int end_shndx);
  1504. template
  1505. unsigned char*
  1506. Sized_relobj_file<32, false>::do_get_output_view(
  1507. unsigned int shndx,
  1508. section_size_type* plen) const;
  1509. #endif
  1510. #ifdef HAVE_TARGET_32_BIG
  1511. template
  1512. void
  1513. Sized_relobj_file<32, true>::do_relocate_sections(
  1514. const Symbol_table* symtab,
  1515. const Layout* layout,
  1516. const unsigned char* pshdrs,
  1517. Output_file* of,
  1518. Views* pviews);
  1519. template
  1520. void
  1521. Sized_relobj_file<32, true>::relocate_section_range(
  1522. const Symbol_table* symtab,
  1523. const Layout* layout,
  1524. const unsigned char* pshdrs,
  1525. Output_file* of,
  1526. Views* pviews,
  1527. unsigned int start_shndx,
  1528. unsigned int end_shndx);
  1529. template
  1530. unsigned char*
  1531. Sized_relobj_file<32, true>::do_get_output_view(
  1532. unsigned int shndx,
  1533. section_size_type* plen) const;
  1534. #endif
  1535. #ifdef HAVE_TARGET_64_LITTLE
  1536. template
  1537. void
  1538. Sized_relobj_file<64, false>::do_relocate_sections(
  1539. const Symbol_table* symtab,
  1540. const Layout* layout,
  1541. const unsigned char* pshdrs,
  1542. Output_file* of,
  1543. Views* pviews);
  1544. template
  1545. void
  1546. Sized_relobj_file<64, false>::relocate_section_range(
  1547. const Symbol_table* symtab,
  1548. const Layout* layout,
  1549. const unsigned char* pshdrs,
  1550. Output_file* of,
  1551. Views* pviews,
  1552. unsigned int start_shndx,
  1553. unsigned int end_shndx);
  1554. template
  1555. unsigned char*
  1556. Sized_relobj_file<64, false>::do_get_output_view(
  1557. unsigned int shndx,
  1558. section_size_type* plen) const;
  1559. #endif
  1560. #ifdef HAVE_TARGET_64_BIG
  1561. template
  1562. void
  1563. Sized_relobj_file<64, true>::do_relocate_sections(
  1564. const Symbol_table* symtab,
  1565. const Layout* layout,
  1566. const unsigned char* pshdrs,
  1567. Output_file* of,
  1568. Views* pviews);
  1569. template
  1570. void
  1571. Sized_relobj_file<64, true>::relocate_section_range(
  1572. const Symbol_table* symtab,
  1573. const Layout* layout,
  1574. const unsigned char* pshdrs,
  1575. Output_file* of,
  1576. Views* pviews,
  1577. unsigned int start_shndx,
  1578. unsigned int end_shndx);
  1579. template
  1580. unsigned char*
  1581. Sized_relobj_file<64, true>::do_get_output_view(
  1582. unsigned int shndx,
  1583. section_size_type* plen) const;
  1584. #endif
  1585. #ifdef HAVE_TARGET_32_LITTLE
  1586. template
  1587. void
  1588. Sized_relobj_file<32, false>::initialize_input_to_output_maps();
  1589. template
  1590. void
  1591. Sized_relobj_file<32, false>::free_input_to_output_maps();
  1592. #endif
  1593. #ifdef HAVE_TARGET_32_BIG
  1594. template
  1595. void
  1596. Sized_relobj_file<32, true>::initialize_input_to_output_maps();
  1597. template
  1598. void
  1599. Sized_relobj_file<32, true>::free_input_to_output_maps();
  1600. #endif
  1601. #ifdef HAVE_TARGET_64_LITTLE
  1602. template
  1603. void
  1604. Sized_relobj_file<64, false>::initialize_input_to_output_maps();
  1605. template
  1606. void
  1607. Sized_relobj_file<64, false>::free_input_to_output_maps();
  1608. #endif
  1609. #ifdef HAVE_TARGET_64_BIG
  1610. template
  1611. void
  1612. Sized_relobj_file<64, true>::initialize_input_to_output_maps();
  1613. template
  1614. void
  1615. Sized_relobj_file<64, true>::free_input_to_output_maps();
  1616. #endif
  1617. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1618. template
  1619. class Merged_symbol_value<32>;
  1620. #endif
  1621. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1622. template
  1623. class Merged_symbol_value<64>;
  1624. #endif
  1625. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1626. template
  1627. class Symbol_value<32>;
  1628. #endif
  1629. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1630. template
  1631. class Symbol_value<64>;
  1632. #endif
  1633. #ifdef HAVE_TARGET_32_LITTLE
  1634. template
  1635. class Track_relocs<32, false>;
  1636. #endif
  1637. #ifdef HAVE_TARGET_32_BIG
  1638. template
  1639. class Track_relocs<32, true>;
  1640. #endif
  1641. #ifdef HAVE_TARGET_64_LITTLE
  1642. template
  1643. class Track_relocs<64, false>;
  1644. #endif
  1645. #ifdef HAVE_TARGET_64_BIG
  1646. template
  1647. class Track_relocs<64, true>;
  1648. #endif
  1649. } // End namespace gold.