reloc.cc 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. // reloc.cc -- relocate input files for gold.
  2. // Copyright (C) 2006-2015 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. // This is a strategy class we use when scanning for --emit-relocs.
  424. template<int sh_type>
  425. class Emit_relocs_strategy
  426. {
  427. public:
  428. // A local non-section symbol.
  429. inline Relocatable_relocs::Reloc_strategy
  430. local_non_section_strategy(unsigned int, Relobj*, unsigned int)
  431. { return Relocatable_relocs::RELOC_COPY; }
  432. // A local section symbol.
  433. inline Relocatable_relocs::Reloc_strategy
  434. local_section_strategy(unsigned int, Relobj*)
  435. {
  436. if (sh_type == elfcpp::SHT_RELA)
  437. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  438. else
  439. {
  440. // The addend is stored in the section contents. Since this
  441. // is not a relocatable link, we are going to apply the
  442. // relocation contents to the section as usual. This means
  443. // that we have no way to record the original addend. If the
  444. // original addend is not zero, there is basically no way for
  445. // the user to handle this correctly. Caveat emptor.
  446. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
  447. }
  448. }
  449. // A global symbol.
  450. inline Relocatable_relocs::Reloc_strategy
  451. global_strategy(unsigned int, Relobj*, unsigned int)
  452. { return Relocatable_relocs::RELOC_COPY; }
  453. };
  454. // Scan the input relocations for --emit-relocs.
  455. template<int size, bool big_endian>
  456. void
  457. Sized_relobj_file<size, big_endian>::emit_relocs_scan(
  458. Symbol_table* symtab,
  459. Layout* layout,
  460. const unsigned char* plocal_syms,
  461. const Read_relocs_data::Relocs_list::iterator& p)
  462. {
  463. Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
  464. gold_assert(rr != NULL);
  465. rr->set_reloc_count(p->reloc_count);
  466. if (p->sh_type == elfcpp::SHT_REL)
  467. this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(symtab, layout,
  468. plocal_syms, p, rr);
  469. else
  470. {
  471. gold_assert(p->sh_type == elfcpp::SHT_RELA);
  472. this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(symtab, layout,
  473. plocal_syms, p, rr);
  474. }
  475. }
  476. // Scan the input relocation for --emit-relocs, templatized on the
  477. // type of the relocation section.
  478. template<int size, bool big_endian>
  479. template<int sh_type>
  480. void
  481. Sized_relobj_file<size, big_endian>::emit_relocs_scan_reltype(
  482. Symbol_table* symtab,
  483. Layout* layout,
  484. const unsigned char* plocal_syms,
  485. const Read_relocs_data::Relocs_list::iterator& p,
  486. Relocatable_relocs* rr)
  487. {
  488. scan_relocatable_relocs<size, big_endian, sh_type,
  489. Emit_relocs_strategy<sh_type> >(
  490. symtab,
  491. layout,
  492. this,
  493. p->data_shndx,
  494. p->contents->data(),
  495. p->reloc_count,
  496. p->output_section,
  497. p->needs_special_offset_handling,
  498. this->local_symbol_count_,
  499. plocal_syms,
  500. rr);
  501. }
  502. // Scan the input relocations for --incremental.
  503. template<int size, bool big_endian>
  504. void
  505. Sized_relobj_file<size, big_endian>::incremental_relocs_scan(
  506. const Read_relocs_data::Relocs_list::iterator& p)
  507. {
  508. if (p->sh_type == elfcpp::SHT_REL)
  509. this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p);
  510. else
  511. {
  512. gold_assert(p->sh_type == elfcpp::SHT_RELA);
  513. this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p);
  514. }
  515. }
  516. // Scan the input relocation for --incremental, templatized on the
  517. // type of the relocation section.
  518. template<int size, bool big_endian>
  519. template<int sh_type>
  520. void
  521. Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype(
  522. const Read_relocs_data::Relocs_list::iterator& p)
  523. {
  524. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  525. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  526. const unsigned char* prelocs = p->contents->data();
  527. size_t reloc_count = p->reloc_count;
  528. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  529. {
  530. Reltype reloc(prelocs);
  531. if (p->needs_special_offset_handling
  532. && !p->output_section->is_input_address_mapped(this, p->data_shndx,
  533. reloc.get_r_offset()))
  534. continue;
  535. typename elfcpp::Elf_types<size>::Elf_WXword r_info =
  536. reloc.get_r_info();
  537. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  538. if (r_sym >= this->local_symbol_count_)
  539. this->count_incremental_reloc(r_sym - this->local_symbol_count_);
  540. }
  541. }
  542. // Relocate the input sections and write out the local symbols.
  543. template<int size, bool big_endian>
  544. void
  545. Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab,
  546. const Layout* layout,
  547. Output_file* of)
  548. {
  549. unsigned int shnum = this->shnum();
  550. // Read the section headers.
  551. const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
  552. shnum * This::shdr_size,
  553. true, true);
  554. Views views;
  555. views.resize(shnum);
  556. // Make two passes over the sections. The first one copies the
  557. // section data to the output file. The second one applies
  558. // relocations.
  559. this->write_sections(layout, pshdrs, of, &views);
  560. // To speed up relocations, we set up hash tables for fast lookup of
  561. // input offsets to output addresses.
  562. this->initialize_input_to_output_maps();
  563. // Apply relocations.
  564. this->relocate_sections(symtab, layout, pshdrs, of, &views);
  565. // After we've done the relocations, we release the hash tables,
  566. // since we no longer need them.
  567. this->free_input_to_output_maps();
  568. // Write out the accumulated views.
  569. for (unsigned int i = 1; i < shnum; ++i)
  570. {
  571. if (views[i].view != NULL)
  572. {
  573. if (views[i].is_ctors_reverse_view)
  574. this->reverse_words(views[i].view, views[i].view_size);
  575. if (!views[i].is_postprocessing_view)
  576. {
  577. if (views[i].is_input_output_view)
  578. of->write_input_output_view(views[i].offset,
  579. views[i].view_size,
  580. views[i].view);
  581. else
  582. of->write_output_view(views[i].offset, views[i].view_size,
  583. views[i].view);
  584. }
  585. }
  586. }
  587. // Write out the local symbols.
  588. this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
  589. layout->symtab_xindex(), layout->dynsym_xindex(),
  590. layout->symtab_section_offset());
  591. }
  592. // Sort a Read_multiple vector by file offset.
  593. struct Read_multiple_compare
  594. {
  595. inline bool
  596. operator()(const File_read::Read_multiple_entry& rme1,
  597. const File_read::Read_multiple_entry& rme2) const
  598. { return rme1.file_offset < rme2.file_offset; }
  599. };
  600. // Write section data to the output file. PSHDRS points to the
  601. // section headers. Record the views in *PVIEWS for use when
  602. // relocating.
  603. template<int size, bool big_endian>
  604. void
  605. Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout,
  606. const unsigned char* pshdrs,
  607. Output_file* of,
  608. Views* pviews)
  609. {
  610. unsigned int shnum = this->shnum();
  611. const Output_sections& out_sections(this->output_sections());
  612. const std::vector<Address>& out_offsets(this->section_offsets());
  613. File_read::Read_multiple rm;
  614. bool is_sorted = true;
  615. const unsigned char* p = pshdrs + This::shdr_size;
  616. for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
  617. {
  618. View_size* pvs = &(*pviews)[i];
  619. pvs->view = NULL;
  620. const Output_section* os = out_sections[i];
  621. if (os == NULL)
  622. continue;
  623. Address output_offset = out_offsets[i];
  624. typename This::Shdr shdr(p);
  625. if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
  626. continue;
  627. if ((parameters->options().relocatable()
  628. || parameters->options().emit_relocs())
  629. && (shdr.get_sh_type() == elfcpp::SHT_REL
  630. || shdr.get_sh_type() == elfcpp::SHT_RELA)
  631. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  632. {
  633. // This is a reloc section in a relocatable link or when
  634. // emitting relocs. We don't need to read the input file.
  635. // The size and file offset are stored in the
  636. // Relocatable_relocs structure.
  637. Relocatable_relocs* rr = this->relocatable_relocs(i);
  638. gold_assert(rr != NULL);
  639. Output_data* posd = rr->output_data();
  640. gold_assert(posd != NULL);
  641. pvs->offset = posd->offset();
  642. pvs->view_size = posd->data_size();
  643. pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
  644. pvs->address = posd->address();
  645. pvs->is_input_output_view = false;
  646. pvs->is_postprocessing_view = false;
  647. pvs->is_ctors_reverse_view = false;
  648. continue;
  649. }
  650. // In the normal case, this input section is simply mapped to
  651. // the output section at offset OUTPUT_OFFSET.
  652. // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
  653. // handled specially--e.g., a .eh_frame section. The relocation
  654. // routines need to check for each reloc where it should be
  655. // applied. For this case, we need an input/output view for the
  656. // entire contents of the section in the output file. We don't
  657. // want to copy the contents of the input section to the output
  658. // section; the output section contents were already written,
  659. // and we waited for them in Relocate_task::is_runnable because
  660. // relocs_must_follow_section_writes is set for the object.
  661. // Regardless of which of the above cases is true, we have to
  662. // check requires_postprocessing of the output section. If that
  663. // is false, then we work with views of the output file
  664. // directly. If it is true, then we work with a separate
  665. // buffer, and the output section is responsible for writing the
  666. // final data to the output file.
  667. off_t output_section_offset;
  668. Address output_section_size;
  669. if (!os->requires_postprocessing())
  670. {
  671. output_section_offset = os->offset();
  672. output_section_size = convert_types<Address, off_t>(os->data_size());
  673. }
  674. else
  675. {
  676. output_section_offset = 0;
  677. output_section_size =
  678. convert_types<Address, off_t>(os->postprocessing_buffer_size());
  679. }
  680. off_t view_start;
  681. section_size_type view_size;
  682. bool must_decompress = false;
  683. if (output_offset != invalid_address)
  684. {
  685. view_start = output_section_offset + output_offset;
  686. view_size = convert_to_section_size_type(shdr.get_sh_size());
  687. section_size_type uncompressed_size;
  688. if (this->section_is_compressed(i, &uncompressed_size))
  689. {
  690. view_size = uncompressed_size;
  691. must_decompress = true;
  692. }
  693. }
  694. else
  695. {
  696. view_start = output_section_offset;
  697. view_size = convert_to_section_size_type(output_section_size);
  698. }
  699. if (view_size == 0)
  700. continue;
  701. gold_assert(output_offset == invalid_address
  702. || output_offset + view_size <= output_section_size);
  703. unsigned char* view;
  704. if (os->requires_postprocessing())
  705. {
  706. unsigned char* buffer = os->postprocessing_buffer();
  707. view = buffer + view_start;
  708. if (output_offset != invalid_address && !must_decompress)
  709. {
  710. off_t sh_offset = shdr.get_sh_offset();
  711. if (!rm.empty() && rm.back().file_offset > sh_offset)
  712. is_sorted = false;
  713. rm.push_back(File_read::Read_multiple_entry(sh_offset,
  714. view_size, view));
  715. }
  716. }
  717. else
  718. {
  719. if (output_offset == invalid_address)
  720. view = of->get_input_output_view(view_start, view_size);
  721. else
  722. {
  723. view = of->get_output_view(view_start, view_size);
  724. if (!must_decompress)
  725. {
  726. off_t sh_offset = shdr.get_sh_offset();
  727. if (!rm.empty() && rm.back().file_offset > sh_offset)
  728. is_sorted = false;
  729. rm.push_back(File_read::Read_multiple_entry(sh_offset,
  730. view_size, view));
  731. }
  732. }
  733. }
  734. if (must_decompress)
  735. {
  736. // Read and decompress the section.
  737. section_size_type len;
  738. const unsigned char* p = this->section_contents(i, &len, false);
  739. if (!decompress_input_section(p, len, view, view_size,
  740. size, big_endian,
  741. shdr.get_sh_flags()))
  742. this->error(_("could not decompress section %s"),
  743. this->section_name(i).c_str());
  744. }
  745. pvs->view = view;
  746. pvs->address = os->address();
  747. if (output_offset != invalid_address)
  748. pvs->address += output_offset;
  749. pvs->offset = view_start;
  750. pvs->view_size = view_size;
  751. pvs->is_input_output_view = output_offset == invalid_address;
  752. pvs->is_postprocessing_view = os->requires_postprocessing();
  753. pvs->is_ctors_reverse_view =
  754. (!parameters->options().relocatable()
  755. && view_size > size / 8
  756. && (strcmp(os->name(), ".init_array") == 0
  757. || strcmp(os->name(), ".fini_array") == 0)
  758. && layout->is_ctors_in_init_array(this, i));
  759. }
  760. // Actually read the data.
  761. if (!rm.empty())
  762. {
  763. if (!is_sorted)
  764. std::sort(rm.begin(), rm.end(), Read_multiple_compare());
  765. this->read_multiple(rm);
  766. }
  767. }
  768. // Relocate section data. VIEWS points to the section data as views
  769. // in the output file.
  770. template<int size, bool big_endian>
  771. void
  772. Sized_relobj_file<size, big_endian>::do_relocate_sections(
  773. const Symbol_table* symtab,
  774. const Layout* layout,
  775. const unsigned char* pshdrs,
  776. Output_file* of,
  777. Views* pviews)
  778. {
  779. unsigned int shnum = this->shnum();
  780. Sized_target<size, big_endian>* target =
  781. parameters->sized_target<size, big_endian>();
  782. const Output_sections& out_sections(this->output_sections());
  783. const std::vector<Address>& out_offsets(this->section_offsets());
  784. Relocate_info<size, big_endian> relinfo;
  785. relinfo.symtab = symtab;
  786. relinfo.layout = layout;
  787. relinfo.object = this;
  788. const unsigned char* p = pshdrs + This::shdr_size;
  789. for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
  790. {
  791. typename This::Shdr shdr(p);
  792. unsigned int sh_type = shdr.get_sh_type();
  793. if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
  794. continue;
  795. off_t sh_size = shdr.get_sh_size();
  796. if (sh_size == 0)
  797. continue;
  798. unsigned int index = this->adjust_shndx(shdr.get_sh_info());
  799. if (index >= this->shnum())
  800. {
  801. this->error(_("relocation section %u has bad info %u"),
  802. i, index);
  803. continue;
  804. }
  805. Output_section* os = out_sections[index];
  806. if (os == NULL)
  807. {
  808. // This relocation section is against a section which we
  809. // discarded.
  810. continue;
  811. }
  812. Address output_offset = out_offsets[index];
  813. gold_assert((*pviews)[index].view != NULL);
  814. if (parameters->options().relocatable())
  815. gold_assert((*pviews)[i].view != NULL);
  816. if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
  817. {
  818. gold_error(_("relocation section %u uses unexpected "
  819. "symbol table %u"),
  820. i, this->adjust_shndx(shdr.get_sh_link()));
  821. continue;
  822. }
  823. const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
  824. sh_size, true, false);
  825. unsigned int reloc_size;
  826. if (sh_type == elfcpp::SHT_REL)
  827. reloc_size = elfcpp::Elf_sizes<size>::rel_size;
  828. else
  829. reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  830. if (reloc_size != shdr.get_sh_entsize())
  831. {
  832. gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
  833. i, static_cast<unsigned long>(shdr.get_sh_entsize()),
  834. reloc_size);
  835. continue;
  836. }
  837. size_t reloc_count = sh_size / reloc_size;
  838. if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
  839. {
  840. gold_error(_("reloc section %u size %lu uneven"),
  841. i, static_cast<unsigned long>(sh_size));
  842. continue;
  843. }
  844. gold_assert(output_offset != invalid_address
  845. || this->relocs_must_follow_section_writes());
  846. relinfo.reloc_shndx = i;
  847. relinfo.reloc_shdr = p;
  848. relinfo.data_shndx = index;
  849. relinfo.data_shdr = pshdrs + index * This::shdr_size;
  850. unsigned char* view = (*pviews)[index].view;
  851. Address address = (*pviews)[index].address;
  852. section_size_type view_size = (*pviews)[index].view_size;
  853. Reloc_symbol_changes* reloc_map = NULL;
  854. if (this->uses_split_stack() && output_offset != invalid_address)
  855. {
  856. typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
  857. if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
  858. this->split_stack_adjust(symtab, pshdrs, sh_type, index,
  859. prelocs, reloc_count, view, view_size,
  860. &reloc_map);
  861. }
  862. if (!parameters->options().relocatable())
  863. {
  864. target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
  865. output_offset == invalid_address,
  866. view, address, view_size, reloc_map);
  867. if (parameters->options().emit_relocs())
  868. {
  869. Relocatable_relocs* rr = this->relocatable_relocs(i);
  870. target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
  871. os, output_offset, rr,
  872. view, address, view_size,
  873. (*pviews)[i].view,
  874. (*pviews)[i].view_size);
  875. }
  876. if (parameters->incremental())
  877. this->incremental_relocs_write(&relinfo, sh_type, prelocs,
  878. reloc_count, os, output_offset, of);
  879. }
  880. else
  881. {
  882. Relocatable_relocs* rr = this->relocatable_relocs(i);
  883. target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
  884. os, output_offset, rr,
  885. view, address, view_size,
  886. (*pviews)[i].view,
  887. (*pviews)[i].view_size);
  888. }
  889. }
  890. }
  891. // Write the incremental relocs.
  892. template<int size, bool big_endian>
  893. void
  894. Sized_relobj_file<size, big_endian>::incremental_relocs_write(
  895. const Relocate_info<size, big_endian>* relinfo,
  896. unsigned int sh_type,
  897. const unsigned char* prelocs,
  898. size_t reloc_count,
  899. Output_section* output_section,
  900. Address output_offset,
  901. Output_file* of)
  902. {
  903. if (sh_type == elfcpp::SHT_REL)
  904. this->incremental_relocs_write_reltype<elfcpp::SHT_REL>(
  905. relinfo,
  906. prelocs,
  907. reloc_count,
  908. output_section,
  909. output_offset,
  910. of);
  911. else
  912. {
  913. gold_assert(sh_type == elfcpp::SHT_RELA);
  914. this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>(
  915. relinfo,
  916. prelocs,
  917. reloc_count,
  918. output_section,
  919. output_offset,
  920. of);
  921. }
  922. }
  923. // Write the incremental relocs, templatized on the type of the
  924. // relocation section.
  925. template<int size, bool big_endian>
  926. template<int sh_type>
  927. void
  928. Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype(
  929. const Relocate_info<size, big_endian>* relinfo,
  930. const unsigned char* prelocs,
  931. size_t reloc_count,
  932. Output_section* output_section,
  933. Address output_offset,
  934. Output_file* of)
  935. {
  936. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
  937. const unsigned int reloc_size =
  938. Reloc_types<sh_type, size, big_endian>::reloc_size;
  939. const unsigned int sizeof_addr = size / 8;
  940. const unsigned int incr_reloc_size =
  941. Incremental_relocs_reader<size, big_endian>::reloc_size;
  942. unsigned int out_shndx = output_section->out_shndx();
  943. // Get a view for the .gnu_incremental_relocs section.
  944. Incremental_inputs* inputs = relinfo->layout->incremental_inputs();
  945. gold_assert(inputs != NULL);
  946. const off_t relocs_off = inputs->relocs_section()->offset();
  947. const off_t relocs_size = inputs->relocs_section()->data_size();
  948. unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
  949. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  950. {
  951. Reloc reloc(prelocs);
  952. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  953. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  954. const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  955. if (r_sym < this->local_symbol_count_)
  956. continue;
  957. // Get the new offset--the location in the output section where
  958. // this relocation should be applied.
  959. Address offset = reloc.get_r_offset();
  960. if (output_offset != invalid_address)
  961. offset += output_offset;
  962. else
  963. {
  964. section_offset_type sot_offset =
  965. convert_types<section_offset_type, Address>(offset);
  966. section_offset_type new_sot_offset =
  967. output_section->output_offset(relinfo->object,
  968. relinfo->data_shndx,
  969. sot_offset);
  970. gold_assert(new_sot_offset != -1);
  971. offset += new_sot_offset;
  972. }
  973. // Get the addend.
  974. typename elfcpp::Elf_types<size>::Elf_Swxword addend;
  975. if (sh_type == elfcpp::SHT_RELA)
  976. addend =
  977. Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc);
  978. else
  979. {
  980. // FIXME: Get the addend for SHT_REL.
  981. addend = 0;
  982. }
  983. // Get the index of the output relocation.
  984. unsigned int reloc_index =
  985. this->next_incremental_reloc_index(r_sym - this->local_symbol_count_);
  986. // Write the relocation.
  987. unsigned char* pov = view + reloc_index * incr_reloc_size;
  988. elfcpp::Swap<32, big_endian>::writeval(pov, r_type);
  989. elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx);
  990. elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset);
  991. elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend);
  992. of->write_output_view(pov - view, incr_reloc_size, view);
  993. }
  994. }
  995. // Create merge hash tables for the local symbols. These are used to
  996. // speed up relocations.
  997. template<int size, bool big_endian>
  998. void
  999. Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps()
  1000. {
  1001. const unsigned int loccount = this->local_symbol_count_;
  1002. for (unsigned int i = 1; i < loccount; ++i)
  1003. {
  1004. Symbol_value<size>& lv(this->local_values_[i]);
  1005. lv.initialize_input_to_output_map(this);
  1006. }
  1007. }
  1008. // Free merge hash tables for the local symbols.
  1009. template<int size, bool big_endian>
  1010. void
  1011. Sized_relobj_file<size, big_endian>::free_input_to_output_maps()
  1012. {
  1013. const unsigned int loccount = this->local_symbol_count_;
  1014. for (unsigned int i = 1; i < loccount; ++i)
  1015. {
  1016. Symbol_value<size>& lv(this->local_values_[i]);
  1017. lv.free_input_to_output_map();
  1018. }
  1019. }
  1020. // If an object was compiled with -fsplit-stack, this is called to
  1021. // check whether any relocations refer to functions defined in objects
  1022. // which were not compiled with -fsplit-stack. If they were, then we
  1023. // need to apply some target-specific adjustments to request
  1024. // additional stack space.
  1025. template<int size, bool big_endian>
  1026. void
  1027. Sized_relobj_file<size, big_endian>::split_stack_adjust(
  1028. const Symbol_table* symtab,
  1029. const unsigned char* pshdrs,
  1030. unsigned int sh_type,
  1031. unsigned int shndx,
  1032. const unsigned char* prelocs,
  1033. size_t reloc_count,
  1034. unsigned char* view,
  1035. section_size_type view_size,
  1036. Reloc_symbol_changes** reloc_map)
  1037. {
  1038. if (sh_type == elfcpp::SHT_REL)
  1039. this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
  1040. prelocs, reloc_count,
  1041. view, view_size,
  1042. reloc_map);
  1043. else
  1044. {
  1045. gold_assert(sh_type == elfcpp::SHT_RELA);
  1046. this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
  1047. prelocs, reloc_count,
  1048. view, view_size,
  1049. reloc_map);
  1050. }
  1051. }
  1052. // Adjust for -fsplit-stack, templatized on the type of the relocation
  1053. // section.
  1054. template<int size, bool big_endian>
  1055. template<int sh_type>
  1056. void
  1057. Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype(
  1058. const Symbol_table* symtab,
  1059. const unsigned char* pshdrs,
  1060. unsigned int shndx,
  1061. const unsigned char* prelocs,
  1062. size_t reloc_count,
  1063. unsigned char* view,
  1064. section_size_type view_size,
  1065. Reloc_symbol_changes** reloc_map)
  1066. {
  1067. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
  1068. const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
  1069. size_t local_count = this->local_symbol_count();
  1070. std::vector<section_offset_type> non_split_refs;
  1071. const unsigned char* pr = prelocs;
  1072. for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
  1073. {
  1074. Reltype reloc(pr);
  1075. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  1076. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  1077. if (r_sym < local_count)
  1078. continue;
  1079. const Symbol* gsym = this->global_symbol(r_sym);
  1080. gold_assert(gsym != NULL);
  1081. if (gsym->is_forwarder())
  1082. gsym = symtab->resolve_forwards(gsym);
  1083. // See if this relocation refers to a function defined in an
  1084. // object compiled without -fsplit-stack. Note that we don't
  1085. // care about the type of relocation--this means that in some
  1086. // cases we will ask for a large stack unnecessarily, but this
  1087. // is not fatal. FIXME: Some targets have symbols which are
  1088. // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
  1089. if (!gsym->is_undefined()
  1090. && gsym->source() == Symbol::FROM_OBJECT
  1091. && !gsym->object()->uses_split_stack())
  1092. {
  1093. unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
  1094. if (parameters->target().is_call_to_non_split(gsym, r_type))
  1095. {
  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. view, view_size, &from, &to);
  1145. if (!from.empty())
  1146. {
  1147. gold_assert(!to.empty());
  1148. Symbol* tosym = NULL;
  1149. // Find relocations in the relevant function which are for
  1150. // FROM.
  1151. pr = prelocs;
  1152. for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
  1153. {
  1154. Reltype reloc(pr);
  1155. typename elfcpp::Elf_types<size>::Elf_WXword r_info =
  1156. reloc.get_r_info();
  1157. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  1158. if (r_sym < local_count)
  1159. continue;
  1160. section_offset_type offset =
  1161. convert_to_section_size_type(reloc.get_r_offset());
  1162. if (offset < p->first
  1163. || (offset
  1164. >= (p->first
  1165. + static_cast<section_offset_type>(p->second))))
  1166. continue;
  1167. const Symbol* gsym = this->global_symbol(r_sym);
  1168. if (from == gsym->name())
  1169. {
  1170. if (tosym == NULL)
  1171. {
  1172. tosym = symtab->lookup(to.c_str());
  1173. if (tosym == NULL)
  1174. {
  1175. this->error(_("could not convert call "
  1176. "to '%s' to '%s'"),
  1177. from.c_str(), to.c_str());
  1178. break;
  1179. }
  1180. }
  1181. if (*reloc_map == NULL)
  1182. *reloc_map = new Reloc_symbol_changes(reloc_count);
  1183. (*reloc_map)->set(i, tosym);
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. // Find all the function in this object defined in section SHNDX.
  1190. // Store their offsets in the section in FUNCTION_OFFSETS.
  1191. template<int size, bool big_endian>
  1192. void
  1193. Sized_relobj_file<size, big_endian>::find_functions(
  1194. const unsigned char* pshdrs,
  1195. unsigned int shndx,
  1196. Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets)
  1197. {
  1198. // We need to read the symbols to find the functions. If we wanted
  1199. // to, we could cache reading the symbols across all sections in the
  1200. // object.
  1201. const unsigned int symtab_shndx = this->symtab_shndx_;
  1202. typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
  1203. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  1204. typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
  1205. symtabshdr.get_sh_size();
  1206. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  1207. sh_size, true, true);
  1208. const int sym_size = This::sym_size;
  1209. const unsigned int symcount = sh_size / sym_size;
  1210. for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
  1211. {
  1212. typename elfcpp::Sym<size, big_endian> isym(psyms);
  1213. // FIXME: Some targets can have functions which do not have type
  1214. // STT_FUNC, e.g., STT_ARM_TFUNC.
  1215. if (isym.get_st_type() != elfcpp::STT_FUNC
  1216. || isym.get_st_size() == 0)
  1217. continue;
  1218. bool is_ordinary;
  1219. Symbol_location loc;
  1220. loc.shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
  1221. &is_ordinary);
  1222. if (!is_ordinary)
  1223. continue;
  1224. loc.object = this;
  1225. loc.offset = isym.get_st_value();
  1226. parameters->target().function_location(&loc);
  1227. if (loc.shndx != shndx)
  1228. continue;
  1229. section_offset_type value =
  1230. convert_to_section_size_type(loc.offset);
  1231. section_size_type fnsize =
  1232. convert_to_section_size_type(isym.get_st_size());
  1233. (*function_offsets)[value] = fnsize;
  1234. }
  1235. }
  1236. // Reverse the words in a section. Used for .ctors sections mapped to
  1237. // .init_array sections. See ctors_sections_in_init_array in
  1238. // layout.cc.
  1239. template<int size, bool big_endian>
  1240. void
  1241. Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view,
  1242. section_size_type view_size)
  1243. {
  1244. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  1245. Valtype* vview = reinterpret_cast<Valtype*>(view);
  1246. section_size_type vview_size = view_size / (size / 8);
  1247. for (section_size_type i = 0; i < vview_size / 2; ++i)
  1248. {
  1249. Valtype tmp = vview[i];
  1250. vview[i] = vview[vview_size - 1 - i];
  1251. vview[vview_size - 1 - i] = tmp;
  1252. }
  1253. }
  1254. // Class Merged_symbol_value.
  1255. template<int size>
  1256. void
  1257. Merged_symbol_value<size>::initialize_input_to_output_map(
  1258. const Relobj* object,
  1259. unsigned int input_shndx)
  1260. {
  1261. object->initialize_input_to_output_map<size>(input_shndx,
  1262. this->output_start_address_,
  1263. &this->output_addresses_);
  1264. }
  1265. // Get the output value corresponding to an input offset if we
  1266. // couldn't find it in the hash table.
  1267. template<int size>
  1268. typename elfcpp::Elf_types<size>::Elf_Addr
  1269. Merged_symbol_value<size>::value_from_output_section(
  1270. const Relobj* object,
  1271. unsigned int input_shndx,
  1272. typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
  1273. {
  1274. section_offset_type output_offset;
  1275. bool found = object->merge_output_offset(input_shndx, input_offset,
  1276. &output_offset);
  1277. // If this assertion fails, it means that some relocation was
  1278. // against a portion of an input merge section which we didn't map
  1279. // to the output file and we didn't explicitly discard. We should
  1280. // always map all portions of input merge sections.
  1281. gold_assert(found);
  1282. if (output_offset == -1)
  1283. return 0;
  1284. else
  1285. return this->output_start_address_ + output_offset;
  1286. }
  1287. // Track_relocs methods.
  1288. // Initialize the class to track the relocs. This gets the object,
  1289. // the reloc section index, and the type of the relocs. This returns
  1290. // false if something goes wrong.
  1291. template<int size, bool big_endian>
  1292. bool
  1293. Track_relocs<size, big_endian>::initialize(
  1294. Object* object,
  1295. unsigned int reloc_shndx,
  1296. unsigned int reloc_type)
  1297. {
  1298. // If RELOC_SHNDX is -1U, it means there is more than one reloc
  1299. // section for the .eh_frame section. We can't handle that case.
  1300. if (reloc_shndx == -1U)
  1301. return false;
  1302. // If RELOC_SHNDX is 0, there is no reloc section.
  1303. if (reloc_shndx == 0)
  1304. return true;
  1305. // Get the contents of the reloc section.
  1306. this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
  1307. if (reloc_type == elfcpp::SHT_REL)
  1308. this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
  1309. else if (reloc_type == elfcpp::SHT_RELA)
  1310. this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
  1311. else
  1312. gold_unreachable();
  1313. if (this->len_ % this->reloc_size_ != 0)
  1314. {
  1315. object->error(_("reloc section size %zu is not a multiple of "
  1316. "reloc size %d\n"),
  1317. static_cast<size_t>(this->len_),
  1318. this->reloc_size_);
  1319. return false;
  1320. }
  1321. return true;
  1322. }
  1323. // Return the offset of the next reloc, or -1 if there isn't one.
  1324. template<int size, bool big_endian>
  1325. off_t
  1326. Track_relocs<size, big_endian>::next_offset() const
  1327. {
  1328. if (this->pos_ >= this->len_)
  1329. return -1;
  1330. // Rel and Rela start out the same, so we can always use Rel to find
  1331. // the r_offset value.
  1332. elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
  1333. return rel.get_r_offset();
  1334. }
  1335. // Return the index of the symbol referenced by the next reloc, or -1U
  1336. // if there aren't any more relocs.
  1337. template<int size, bool big_endian>
  1338. unsigned int
  1339. Track_relocs<size, big_endian>::next_symndx() const
  1340. {
  1341. if (this->pos_ >= this->len_)
  1342. return -1U;
  1343. // Rel and Rela start out the same, so we can use Rel to find the
  1344. // symbol index.
  1345. elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
  1346. return elfcpp::elf_r_sym<size>(rel.get_r_info());
  1347. }
  1348. // Return the addend of the next reloc, or 0 if there isn't one.
  1349. template<int size, bool big_endian>
  1350. uint64_t
  1351. Track_relocs<size, big_endian>::next_addend() const
  1352. {
  1353. if (this->pos_ >= this->len_)
  1354. return 0;
  1355. if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size)
  1356. return 0;
  1357. elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_);
  1358. return rela.get_r_addend();
  1359. }
  1360. // Advance to the next reloc whose r_offset is greater than or equal
  1361. // to OFFSET. Return the number of relocs we skip.
  1362. template<int size, bool big_endian>
  1363. int
  1364. Track_relocs<size, big_endian>::advance(off_t offset)
  1365. {
  1366. int ret = 0;
  1367. while (this->pos_ < this->len_)
  1368. {
  1369. // Rel and Rela start out the same, so we can always use Rel to
  1370. // find the r_offset value.
  1371. elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
  1372. if (static_cast<off_t>(rel.get_r_offset()) >= offset)
  1373. break;
  1374. ++ret;
  1375. this->pos_ += this->reloc_size_;
  1376. }
  1377. return ret;
  1378. }
  1379. // Instantiate the templates we need.
  1380. #ifdef HAVE_TARGET_32_LITTLE
  1381. template
  1382. void
  1383. Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd);
  1384. #endif
  1385. #ifdef HAVE_TARGET_32_BIG
  1386. template
  1387. void
  1388. Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd);
  1389. #endif
  1390. #ifdef HAVE_TARGET_64_LITTLE
  1391. template
  1392. void
  1393. Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd);
  1394. #endif
  1395. #ifdef HAVE_TARGET_64_BIG
  1396. template
  1397. void
  1398. Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd);
  1399. #endif
  1400. #ifdef HAVE_TARGET_32_LITTLE
  1401. template
  1402. void
  1403. Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab,
  1404. Layout* layout,
  1405. Read_relocs_data* rd);
  1406. #endif
  1407. #ifdef HAVE_TARGET_32_BIG
  1408. template
  1409. void
  1410. Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab,
  1411. Layout* layout,
  1412. Read_relocs_data* rd);
  1413. #endif
  1414. #ifdef HAVE_TARGET_64_LITTLE
  1415. template
  1416. void
  1417. Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab,
  1418. Layout* layout,
  1419. Read_relocs_data* rd);
  1420. #endif
  1421. #ifdef HAVE_TARGET_64_BIG
  1422. template
  1423. void
  1424. Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab,
  1425. Layout* layout,
  1426. Read_relocs_data* rd);
  1427. #endif
  1428. #ifdef HAVE_TARGET_32_LITTLE
  1429. template
  1430. void
  1431. Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab,
  1432. Layout* layout,
  1433. Read_relocs_data* rd);
  1434. #endif
  1435. #ifdef HAVE_TARGET_32_BIG
  1436. template
  1437. void
  1438. Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab,
  1439. Layout* layout,
  1440. Read_relocs_data* rd);
  1441. #endif
  1442. #ifdef HAVE_TARGET_64_LITTLE
  1443. template
  1444. void
  1445. Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab,
  1446. Layout* layout,
  1447. Read_relocs_data* rd);
  1448. #endif
  1449. #ifdef HAVE_TARGET_64_BIG
  1450. template
  1451. void
  1452. Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab,
  1453. Layout* layout,
  1454. Read_relocs_data* rd);
  1455. #endif
  1456. #ifdef HAVE_TARGET_32_LITTLE
  1457. template
  1458. void
  1459. Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab,
  1460. const Layout* layout,
  1461. Output_file* of);
  1462. #endif
  1463. #ifdef HAVE_TARGET_32_BIG
  1464. template
  1465. void
  1466. Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab,
  1467. const Layout* layout,
  1468. Output_file* of);
  1469. #endif
  1470. #ifdef HAVE_TARGET_64_LITTLE
  1471. template
  1472. void
  1473. Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab,
  1474. const Layout* layout,
  1475. Output_file* of);
  1476. #endif
  1477. #ifdef HAVE_TARGET_64_BIG
  1478. template
  1479. void
  1480. Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab,
  1481. const Layout* layout,
  1482. Output_file* of);
  1483. #endif
  1484. #ifdef HAVE_TARGET_32_LITTLE
  1485. template
  1486. void
  1487. Sized_relobj_file<32, false>::do_relocate_sections(
  1488. const Symbol_table* symtab,
  1489. const Layout* layout,
  1490. const unsigned char* pshdrs,
  1491. Output_file* of,
  1492. Views* pviews);
  1493. #endif
  1494. #ifdef HAVE_TARGET_32_BIG
  1495. template
  1496. void
  1497. Sized_relobj_file<32, true>::do_relocate_sections(
  1498. const Symbol_table* symtab,
  1499. const Layout* layout,
  1500. const unsigned char* pshdrs,
  1501. Output_file* of,
  1502. Views* pviews);
  1503. #endif
  1504. #ifdef HAVE_TARGET_64_LITTLE
  1505. template
  1506. void
  1507. Sized_relobj_file<64, false>::do_relocate_sections(
  1508. const Symbol_table* symtab,
  1509. const Layout* layout,
  1510. const unsigned char* pshdrs,
  1511. Output_file* of,
  1512. Views* pviews);
  1513. #endif
  1514. #ifdef HAVE_TARGET_64_BIG
  1515. template
  1516. void
  1517. Sized_relobj_file<64, true>::do_relocate_sections(
  1518. const Symbol_table* symtab,
  1519. const Layout* layout,
  1520. const unsigned char* pshdrs,
  1521. Output_file* of,
  1522. Views* pviews);
  1523. #endif
  1524. #ifdef HAVE_TARGET_32_LITTLE
  1525. template
  1526. void
  1527. Sized_relobj_file<32, false>::initialize_input_to_output_maps();
  1528. template
  1529. void
  1530. Sized_relobj_file<32, false>::free_input_to_output_maps();
  1531. #endif
  1532. #ifdef HAVE_TARGET_32_BIG
  1533. template
  1534. void
  1535. Sized_relobj_file<32, true>::initialize_input_to_output_maps();
  1536. template
  1537. void
  1538. Sized_relobj_file<32, true>::free_input_to_output_maps();
  1539. #endif
  1540. #ifdef HAVE_TARGET_64_LITTLE
  1541. template
  1542. void
  1543. Sized_relobj_file<64, false>::initialize_input_to_output_maps();
  1544. template
  1545. void
  1546. Sized_relobj_file<64, false>::free_input_to_output_maps();
  1547. #endif
  1548. #ifdef HAVE_TARGET_64_BIG
  1549. template
  1550. void
  1551. Sized_relobj_file<64, true>::initialize_input_to_output_maps();
  1552. template
  1553. void
  1554. Sized_relobj_file<64, true>::free_input_to_output_maps();
  1555. #endif
  1556. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1557. template
  1558. class Merged_symbol_value<32>;
  1559. #endif
  1560. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1561. template
  1562. class Merged_symbol_value<64>;
  1563. #endif
  1564. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1565. template
  1566. class Symbol_value<32>;
  1567. #endif
  1568. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1569. template
  1570. class Symbol_value<64>;
  1571. #endif
  1572. #ifdef HAVE_TARGET_32_LITTLE
  1573. template
  1574. class Track_relocs<32, false>;
  1575. #endif
  1576. #ifdef HAVE_TARGET_32_BIG
  1577. template
  1578. class Track_relocs<32, true>;
  1579. #endif
  1580. #ifdef HAVE_TARGET_64_LITTLE
  1581. template
  1582. class Track_relocs<64, false>;
  1583. #endif
  1584. #ifdef HAVE_TARGET_64_BIG
  1585. template
  1586. class Track_relocs<64, true>;
  1587. #endif
  1588. } // End namespace gold.