reloc.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. // reloc.h -- relocate input files for gold -*- C++ -*-
  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. #ifndef GOLD_RELOC_H
  18. #define GOLD_RELOC_H
  19. #include <vector>
  20. #ifdef HAVE_BYTESWAP_H
  21. #include <byteswap.h>
  22. #endif
  23. #include "elfcpp.h"
  24. #include "workqueue.h"
  25. namespace gold
  26. {
  27. class General_options;
  28. class Object;
  29. class Relobj;
  30. struct Read_relocs_data;
  31. class Symbol;
  32. class Layout;
  33. class Output_data;
  34. class Output_section;
  35. template<int size>
  36. class Sized_symbol;
  37. template<int size, bool big_endian>
  38. class Sized_relobj_file;
  39. template<int size>
  40. class Symbol_value;
  41. template<int sh_type, bool dynamic, int size, bool big_endian>
  42. class Output_data_reloc;
  43. // A class to read the relocations for an object file, and then queue
  44. // up a task to see if they require any GOT/PLT/COPY relocations in
  45. // the symbol table.
  46. class Read_relocs : public Task
  47. {
  48. public:
  49. // THIS_BLOCKER and NEXT_BLOCKER are passed along to a Scan_relocs
  50. // or Gc_process_relocs task, so that they run in a deterministic
  51. // order.
  52. Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
  53. Task_token* this_blocker, Task_token* next_blocker)
  54. : symtab_(symtab), layout_(layout), object_(object),
  55. this_blocker_(this_blocker), next_blocker_(next_blocker)
  56. { }
  57. // The standard Task methods.
  58. Task_token*
  59. is_runnable();
  60. void
  61. locks(Task_locker*);
  62. void
  63. run(Workqueue*);
  64. std::string
  65. get_name() const;
  66. private:
  67. Symbol_table* symtab_;
  68. Layout* layout_;
  69. Relobj* object_;
  70. Task_token* this_blocker_;
  71. Task_token* next_blocker_;
  72. };
  73. // Process the relocs to figure out which sections are garbage.
  74. // Very similar to scan relocs.
  75. class Gc_process_relocs : public Task
  76. {
  77. public:
  78. // THIS_BLOCKER prevents this task from running until the previous
  79. // one is finished. NEXT_BLOCKER prevents the next task from
  80. // running.
  81. Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
  82. Read_relocs_data* rd, Task_token* this_blocker,
  83. Task_token* next_blocker)
  84. : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
  85. this_blocker_(this_blocker), next_blocker_(next_blocker)
  86. { }
  87. ~Gc_process_relocs();
  88. // The standard Task methods.
  89. Task_token*
  90. is_runnable();
  91. void
  92. locks(Task_locker*);
  93. void
  94. run(Workqueue*);
  95. std::string
  96. get_name() const;
  97. private:
  98. Symbol_table* symtab_;
  99. Layout* layout_;
  100. Relobj* object_;
  101. Read_relocs_data* rd_;
  102. Task_token* this_blocker_;
  103. Task_token* next_blocker_;
  104. };
  105. // Scan the relocations for an object to see if they require any
  106. // GOT/PLT/COPY relocations.
  107. class Scan_relocs : public Task
  108. {
  109. public:
  110. // THIS_BLOCKER prevents this task from running until the previous
  111. // one is finished. NEXT_BLOCKER prevents the next task from
  112. // running.
  113. Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
  114. Read_relocs_data* rd, Task_token* this_blocker,
  115. Task_token* next_blocker)
  116. : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
  117. this_blocker_(this_blocker), next_blocker_(next_blocker)
  118. { }
  119. ~Scan_relocs();
  120. // The standard Task methods.
  121. Task_token*
  122. is_runnable();
  123. void
  124. locks(Task_locker*);
  125. void
  126. run(Workqueue*);
  127. std::string
  128. get_name() const;
  129. private:
  130. Symbol_table* symtab_;
  131. Layout* layout_;
  132. Relobj* object_;
  133. Read_relocs_data* rd_;
  134. Task_token* this_blocker_;
  135. Task_token* next_blocker_;
  136. };
  137. // A class to perform all the relocations for an object file.
  138. class Relocate_task : public Task
  139. {
  140. public:
  141. Relocate_task(const Symbol_table* symtab, const Layout* layout,
  142. Relobj* object, Output_file* of,
  143. Task_token* input_sections_blocker,
  144. Task_token* output_sections_blocker, Task_token* final_blocker)
  145. : symtab_(symtab), layout_(layout), object_(object), of_(of),
  146. input_sections_blocker_(input_sections_blocker),
  147. output_sections_blocker_(output_sections_blocker),
  148. final_blocker_(final_blocker)
  149. { }
  150. // The standard Task methods.
  151. Task_token*
  152. is_runnable();
  153. void
  154. locks(Task_locker*);
  155. void
  156. run(Workqueue*);
  157. std::string
  158. get_name() const;
  159. private:
  160. const Symbol_table* symtab_;
  161. const Layout* layout_;
  162. Relobj* object_;
  163. Output_file* of_;
  164. Task_token* input_sections_blocker_;
  165. Task_token* output_sections_blocker_;
  166. Task_token* final_blocker_;
  167. };
  168. // During a relocatable link, this class records how relocations
  169. // should be handled for a single input reloc section. An instance of
  170. // this class is created while scanning relocs, and it is used while
  171. // processing relocs.
  172. class Relocatable_relocs
  173. {
  174. public:
  175. // We use a vector of unsigned char to indicate how the input relocs
  176. // should be handled. Each element is one of the following values.
  177. // We create this vector when we initially scan the relocations.
  178. enum Reloc_strategy
  179. {
  180. // Copy the input reloc. Don't modify it other than updating the
  181. // r_offset field and the r_sym part of the r_info field.
  182. RELOC_COPY,
  183. // Copy the input reloc which is against an STT_SECTION symbol.
  184. // Update the r_offset and r_sym part of the r_info field. Adjust
  185. // the addend by subtracting the value of the old local symbol and
  186. // adding the value of the new local symbol. The addend is in the
  187. // SHT_RELA reloc and the contents of the data section do not need
  188. // to be changed.
  189. RELOC_ADJUST_FOR_SECTION_RELA,
  190. // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
  191. // adjusted.
  192. RELOC_ADJUST_FOR_SECTION_0,
  193. // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
  194. // section need to be changed. The number indicates the number of
  195. // bytes in the addend in the section contents.
  196. RELOC_ADJUST_FOR_SECTION_1,
  197. RELOC_ADJUST_FOR_SECTION_2,
  198. RELOC_ADJUST_FOR_SECTION_4,
  199. RELOC_ADJUST_FOR_SECTION_8,
  200. // Like RELOC_ADJUST_FOR_SECTION_4 but for unaligned relocs.
  201. RELOC_ADJUST_FOR_SECTION_4_UNALIGNED,
  202. // Discard the input reloc--process it completely when relocating
  203. // the data section contents.
  204. RELOC_DISCARD,
  205. // An input reloc which is not discarded, but which requires
  206. // target specific processing in order to update it.
  207. RELOC_SPECIAL
  208. };
  209. Relocatable_relocs()
  210. : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
  211. { }
  212. // Record the number of relocs.
  213. void
  214. set_reloc_count(size_t reloc_count)
  215. { this->reloc_strategies_.reserve(reloc_count); }
  216. // Record what to do for the next reloc.
  217. void
  218. set_next_reloc_strategy(Reloc_strategy strategy)
  219. {
  220. this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
  221. if (strategy != RELOC_DISCARD)
  222. ++this->output_reloc_count_;
  223. }
  224. // Record the Output_data associated with this reloc section.
  225. void
  226. set_output_data(Output_data* posd)
  227. {
  228. gold_assert(this->posd_ == NULL);
  229. this->posd_ = posd;
  230. }
  231. // Return the Output_data associated with this reloc section.
  232. Output_data*
  233. output_data() const
  234. { return this->posd_; }
  235. // Return what to do for reloc I.
  236. Reloc_strategy
  237. strategy(unsigned int i) const
  238. {
  239. gold_assert(i < this->reloc_strategies_.size());
  240. return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
  241. }
  242. // Set the strategy for reloc I.
  243. void
  244. set_strategy(unsigned int i, Reloc_strategy strategy)
  245. {
  246. gold_assert(i < this->reloc_strategies_.size());
  247. this->reloc_strategies_[i] = strategy;
  248. }
  249. // Return the number of relocations to create in the output file.
  250. size_t
  251. output_reloc_count() const
  252. { return this->output_reloc_count_; }
  253. private:
  254. typedef std::vector<unsigned char> Reloc_strategies;
  255. // The strategies for the input reloc. There is one entry in this
  256. // vector for each relocation in the input section.
  257. Reloc_strategies reloc_strategies_;
  258. // The number of relocations to be created in the output file.
  259. size_t output_reloc_count_;
  260. // The output data structure associated with this relocation.
  261. Output_data* posd_;
  262. };
  263. template<int valsize>
  264. class Bits;
  265. // Standard relocation routines which are used on many targets. Here
  266. // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
  267. template<int size, bool big_endian>
  268. class Relocate_functions
  269. {
  270. public:
  271. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  272. typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addendtype;
  273. enum Overflow_check
  274. {
  275. // No overflow checking.
  276. CHECK_NONE,
  277. // Check for overflow of a signed value.
  278. CHECK_SIGNED,
  279. // Check for overflow of an unsigned value.
  280. CHECK_UNSIGNED,
  281. // Check for overflow of a signed or unsigned value.
  282. // (i.e., no error if either signed or unsigned fits.)
  283. CHECK_SIGNED_OR_UNSIGNED
  284. };
  285. enum Reloc_status
  286. {
  287. RELOC_OK,
  288. RELOC_OVERFLOW
  289. };
  290. private:
  291. // Check for overflow.
  292. template<int valsize>
  293. static inline Reloc_status
  294. check_overflow(Address value, Overflow_check check)
  295. {
  296. switch (check)
  297. {
  298. case CHECK_SIGNED:
  299. if (size == 32)
  300. return (Bits<valsize>::has_overflow32(value)
  301. ? RELOC_OVERFLOW
  302. : RELOC_OK);
  303. else
  304. return (Bits<valsize>::has_overflow(value)
  305. ? RELOC_OVERFLOW
  306. : RELOC_OK);
  307. case CHECK_UNSIGNED:
  308. if (size == 32)
  309. return (Bits<valsize>::has_unsigned_overflow32(value)
  310. ? RELOC_OVERFLOW
  311. : RELOC_OK);
  312. else
  313. return (Bits<valsize>::has_unsigned_overflow(value)
  314. ? RELOC_OVERFLOW
  315. : RELOC_OK);
  316. case CHECK_SIGNED_OR_UNSIGNED:
  317. if (size == 32)
  318. return (Bits<valsize>::has_signed_unsigned_overflow32(value)
  319. ? RELOC_OVERFLOW
  320. : RELOC_OK);
  321. else
  322. return (Bits<valsize>::has_signed_unsigned_overflow64(value)
  323. ? RELOC_OVERFLOW
  324. : RELOC_OK);
  325. case CHECK_NONE:
  326. default:
  327. return RELOC_OK;
  328. }
  329. }
  330. // Do a simple relocation with the addend in the section contents.
  331. // VALSIZE is the size of the value.
  332. template<int valsize>
  333. static inline Reloc_status
  334. rel(unsigned char* view, Address value, Overflow_check check)
  335. {
  336. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  337. Valtype* wv = reinterpret_cast<Valtype*>(view);
  338. Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
  339. value += addend;
  340. elfcpp::Swap<valsize, big_endian>::
  341. writeval(wv, static_cast<Valtype>(value));
  342. return check_overflow<valsize>(value, check);
  343. }
  344. // Like the above but for relocs at unaligned addresses.
  345. template<int valsize>
  346. static inline Reloc_status
  347. rel_unaligned(unsigned char* view, Address value, Overflow_check check)
  348. {
  349. typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
  350. Valtype;
  351. Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
  352. value += addend;
  353. elfcpp::Swap_unaligned<valsize, big_endian>::
  354. writeval(view, static_cast<Valtype>(value));
  355. return check_overflow<valsize>(value, check);
  356. }
  357. // Do a simple relocation using a Symbol_value with the addend in
  358. // the section contents. VALSIZE is the size of the value to
  359. // relocate.
  360. template<int valsize>
  361. static inline Reloc_status
  362. rel(unsigned char* view,
  363. const Sized_relobj_file<size, big_endian>* object,
  364. const Symbol_value<size>* psymval,
  365. Overflow_check check)
  366. {
  367. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  368. Valtype* wv = reinterpret_cast<Valtype*>(view);
  369. Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
  370. Address value = psymval->value(object, addend);
  371. elfcpp::Swap<valsize, big_endian>::
  372. writeval(wv, static_cast<Valtype>(value));
  373. return check_overflow<valsize>(value, check);
  374. }
  375. // Like the above but for relocs at unaligned addresses.
  376. template<int valsize>
  377. static inline Reloc_status
  378. rel_unaligned(unsigned char* view,
  379. const Sized_relobj_file<size, big_endian>* object,
  380. const Symbol_value<size>* psymval,
  381. Overflow_check check)
  382. {
  383. typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
  384. Valtype;
  385. Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
  386. Address value = psymval->value(object, addend);
  387. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
  388. return check_overflow<valsize>(value, check);
  389. }
  390. // Do a simple relocation with the addend in the relocation.
  391. // VALSIZE is the size of the value.
  392. template<int valsize>
  393. static inline Reloc_status
  394. rela(unsigned char* view, Address value, Addendtype addend,
  395. Overflow_check check)
  396. {
  397. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  398. Valtype* wv = reinterpret_cast<Valtype*>(view);
  399. value += addend;
  400. elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
  401. return check_overflow<valsize>(value, check);
  402. }
  403. // Do a simple relocation using a symbol value with the addend in
  404. // the relocation. VALSIZE is the size of the value.
  405. template<int valsize>
  406. static inline Reloc_status
  407. rela(unsigned char* view,
  408. const Sized_relobj_file<size, big_endian>* object,
  409. const Symbol_value<size>* psymval,
  410. Addendtype addend,
  411. Overflow_check check)
  412. {
  413. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  414. Valtype* wv = reinterpret_cast<Valtype*>(view);
  415. Address value = psymval->value(object, addend);
  416. elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
  417. return check_overflow<valsize>(value, check);
  418. }
  419. // Do a simple PC relative relocation with the addend in the section
  420. // contents. VALSIZE is the size of the value.
  421. template<int valsize>
  422. static inline Reloc_status
  423. pcrel(unsigned char* view, Address value, Address address,
  424. Overflow_check check)
  425. {
  426. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  427. Valtype* wv = reinterpret_cast<Valtype*>(view);
  428. Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
  429. value = value + addend - address;
  430. elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
  431. return check_overflow<valsize>(value, check);
  432. }
  433. // Like the above but for relocs at unaligned addresses.
  434. template<int valsize>
  435. static inline Reloc_status
  436. pcrel_unaligned(unsigned char* view, Address value, Address address,
  437. Overflow_check check)
  438. {
  439. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  440. Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
  441. value = value + addend - address;
  442. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
  443. return check_overflow<valsize>(value, check);
  444. }
  445. // Do a simple PC relative relocation with a Symbol_value with the
  446. // addend in the section contents. VALSIZE is the size of the
  447. // value.
  448. template<int valsize>
  449. static inline Reloc_status
  450. pcrel(unsigned char* view,
  451. const Sized_relobj_file<size, big_endian>* object,
  452. const Symbol_value<size>* psymval,
  453. Address address,
  454. Overflow_check check)
  455. {
  456. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  457. Valtype* wv = reinterpret_cast<Valtype*>(view);
  458. Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv);
  459. Address value = psymval->value(object, addend) - address;
  460. elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
  461. return check_overflow<valsize>(value, check);
  462. }
  463. // Do a simple PC relative relocation with the addend in the
  464. // relocation. VALSIZE is the size of the value.
  465. template<int valsize>
  466. static inline Reloc_status
  467. pcrela(unsigned char* view, Address value, Addendtype addend, Address address,
  468. Overflow_check check)
  469. {
  470. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  471. Valtype* wv = reinterpret_cast<Valtype*>(view);
  472. value = value + addend - address;
  473. elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
  474. return check_overflow<valsize>(value, check);
  475. }
  476. // Do a simple PC relative relocation with a Symbol_value with the
  477. // addend in the relocation. VALSIZE is the size of the value.
  478. template<int valsize>
  479. static inline Reloc_status
  480. pcrela(unsigned char* view,
  481. const Sized_relobj_file<size, big_endian>* object,
  482. const Symbol_value<size>* psymval,
  483. Addendtype addend,
  484. Address address,
  485. Overflow_check check)
  486. {
  487. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  488. Valtype* wv = reinterpret_cast<Valtype*>(view);
  489. Address value = psymval->value(object, addend) - address;
  490. elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
  491. return check_overflow<valsize>(value, check);
  492. }
  493. typedef Relocate_functions<size, big_endian> This;
  494. public:
  495. // Do a simple 8-bit REL relocation with the addend in the section
  496. // contents.
  497. static inline void
  498. rel8(unsigned char* view, Address value)
  499. { This::template rel<8>(view, value, CHECK_NONE); }
  500. static inline Reloc_status
  501. rel8_check(unsigned char* view, Address value, Overflow_check check)
  502. { return This::template rel<8>(view, value, check); }
  503. static inline void
  504. rel8(unsigned char* view,
  505. const Sized_relobj_file<size, big_endian>* object,
  506. const Symbol_value<size>* psymval)
  507. { This::template rel<8>(view, object, psymval, CHECK_NONE); }
  508. static inline Reloc_status
  509. rel8_check(unsigned char* view,
  510. const Sized_relobj_file<size, big_endian>* object,
  511. const Symbol_value<size>* psymval,
  512. Overflow_check check)
  513. { return This::template rel<8>(view, object, psymval, check); }
  514. // Do an 8-bit RELA relocation with the addend in the relocation.
  515. static inline void
  516. rela8(unsigned char* view, Address value, Addendtype addend)
  517. { This::template rela<8>(view, value, addend, CHECK_NONE); }
  518. static inline Reloc_status
  519. rela8_check(unsigned char* view, Address value, Addendtype addend,
  520. Overflow_check check)
  521. { return This::template rela<8>(view, value, addend, check); }
  522. static inline void
  523. rela8(unsigned char* view,
  524. const Sized_relobj_file<size, big_endian>* object,
  525. const Symbol_value<size>* psymval,
  526. Addendtype addend)
  527. { This::template rela<8>(view, object, psymval, addend, CHECK_NONE); }
  528. static inline Reloc_status
  529. rela8_check(unsigned char* view,
  530. const Sized_relobj_file<size, big_endian>* object,
  531. const Symbol_value<size>* psymval,
  532. Addendtype addend,
  533. Overflow_check check)
  534. { return This::template rela<8>(view, object, psymval, addend, check); }
  535. // Do a simple 8-bit PC relative relocation with the addend in the
  536. // section contents.
  537. static inline void
  538. pcrel8(unsigned char* view, unsigned char value, Address address)
  539. { This::template pcrel<8>(view, value, address, CHECK_NONE); }
  540. static inline Reloc_status
  541. pcrel8_check(unsigned char* view, unsigned char value, Address address,
  542. Overflow_check check)
  543. { return This::template pcrel<8>(view, value, address, check); }
  544. static inline void
  545. pcrel8(unsigned char* view,
  546. const Sized_relobj_file<size, big_endian>* object,
  547. const Symbol_value<size>* psymval,
  548. Address address)
  549. { This::template pcrel<8>(view, object, psymval, address, CHECK_NONE); }
  550. static inline Reloc_status
  551. pcrel8_check(unsigned char* view,
  552. const Sized_relobj_file<size, big_endian>* object,
  553. const Symbol_value<size>* psymval,
  554. Address address,
  555. Overflow_check check)
  556. { return This::template pcrel<8>(view, object, psymval, address, check); }
  557. // Do a simple 8-bit PC relative RELA relocation with the addend in
  558. // the reloc.
  559. static inline void
  560. pcrela8(unsigned char* view, Address value, Addendtype addend,
  561. Address address)
  562. { This::template pcrela<8>(view, value, addend, address, CHECK_NONE); }
  563. static inline Reloc_status
  564. pcrela8_check(unsigned char* view, Address value, Addendtype addend,
  565. Address address, Overflow_check check)
  566. { return This::template pcrela<8>(view, value, addend, address, check); }
  567. static inline void
  568. pcrela8(unsigned char* view,
  569. const Sized_relobj_file<size, big_endian>* object,
  570. const Symbol_value<size>* psymval,
  571. Addendtype addend,
  572. Address address)
  573. { This::template pcrela<8>(view, object, psymval, addend, address,
  574. CHECK_NONE); }
  575. static inline Reloc_status
  576. pcrela8_check(unsigned char* view,
  577. const Sized_relobj_file<size, big_endian>* object,
  578. const Symbol_value<size>* psymval,
  579. Addendtype addend,
  580. Address address,
  581. Overflow_check check)
  582. { return This::template pcrela<8>(view, object, psymval, addend, address,
  583. check); }
  584. // Do a simple 16-bit REL relocation with the addend in the section
  585. // contents.
  586. static inline void
  587. rel16(unsigned char* view, Address value)
  588. { This::template rel<16>(view, value, CHECK_NONE); }
  589. static inline Reloc_status
  590. rel16_check(unsigned char* view, Address value, Overflow_check check)
  591. { return This::template rel<16>(view, value, check); }
  592. static inline void
  593. rel16(unsigned char* view,
  594. const Sized_relobj_file<size, big_endian>* object,
  595. const Symbol_value<size>* psymval)
  596. { This::template rel<16>(view, object, psymval, CHECK_NONE); }
  597. static inline Reloc_status
  598. rel16_check(unsigned char* view,
  599. const Sized_relobj_file<size, big_endian>* object,
  600. const Symbol_value<size>* psymval,
  601. Overflow_check check)
  602. { return This::template rel<16>(view, object, psymval, check); }
  603. // Do an 16-bit RELA relocation with the addend in the relocation.
  604. static inline void
  605. rela16(unsigned char* view, Address value, Addendtype addend)
  606. { This::template rela<16>(view, value, addend, CHECK_NONE); }
  607. static inline Reloc_status
  608. rela16_check(unsigned char* view, Address value, Addendtype addend,
  609. Overflow_check check)
  610. { return This::template rela<16>(view, value, addend, check); }
  611. static inline void
  612. rela16(unsigned char* view,
  613. const Sized_relobj_file<size, big_endian>* object,
  614. const Symbol_value<size>* psymval,
  615. Addendtype addend)
  616. { This::template rela<16>(view, object, psymval, addend, CHECK_NONE); }
  617. static inline Reloc_status
  618. rela16_check(unsigned char* view,
  619. const Sized_relobj_file<size, big_endian>* object,
  620. const Symbol_value<size>* psymval,
  621. Addendtype addend,
  622. Overflow_check check)
  623. { return This::template rela<16>(view, object, psymval, addend, check); }
  624. // Do a simple 16-bit PC relative REL relocation with the addend in
  625. // the section contents.
  626. static inline void
  627. pcrel16(unsigned char* view, Address value, Address address)
  628. { This::template pcrel<16>(view, value, address, CHECK_NONE); }
  629. static inline Reloc_status
  630. pcrel16_check(unsigned char* view, Address value, Address address,
  631. Overflow_check check)
  632. { return This::template pcrel<16>(view, value, address, check); }
  633. static inline void
  634. pcrel16(unsigned char* view,
  635. const Sized_relobj_file<size, big_endian>* object,
  636. const Symbol_value<size>* psymval,
  637. Address address)
  638. { This::template pcrel<16>(view, object, psymval, address, CHECK_NONE); }
  639. static inline Reloc_status
  640. pcrel16_check(unsigned char* view,
  641. const Sized_relobj_file<size, big_endian>* object,
  642. const Symbol_value<size>* psymval,
  643. Address address,
  644. Overflow_check check)
  645. { return This::template pcrel<16>(view, object, psymval, address, check); }
  646. // Do a simple 16-bit PC relative RELA relocation with the addend in
  647. // the reloc.
  648. static inline void
  649. pcrela16(unsigned char* view, Address value, Addendtype addend,
  650. Address address)
  651. { This::template pcrela<16>(view, value, addend, address, CHECK_NONE); }
  652. static inline Reloc_status
  653. pcrela16_check(unsigned char* view, Address value, Addendtype addend,
  654. Address address, Overflow_check check)
  655. { return This::template pcrela<16>(view, value, addend, address, check); }
  656. static inline void
  657. pcrela16(unsigned char* view,
  658. const Sized_relobj_file<size, big_endian>* object,
  659. const Symbol_value<size>* psymval,
  660. Addendtype addend,
  661. Address address)
  662. { This::template pcrela<16>(view, object, psymval, addend, address,
  663. CHECK_NONE); }
  664. static inline Reloc_status
  665. pcrela16_check(unsigned char* view,
  666. const Sized_relobj_file<size, big_endian>* object,
  667. const Symbol_value<size>* psymval,
  668. Addendtype addend,
  669. Address address,
  670. Overflow_check check)
  671. { return This::template pcrela<16>(view, object, psymval, addend, address,
  672. check); }
  673. // Do a simple 32-bit REL relocation with the addend in the section
  674. // contents.
  675. static inline void
  676. rel32(unsigned char* view, Address value)
  677. { This::template rel<32>(view, value, CHECK_NONE); }
  678. static inline Reloc_status
  679. rel32_check(unsigned char* view, Address value, Overflow_check check)
  680. { return This::template rel<32>(view, value, check); }
  681. // Like above but for relocs at unaligned addresses.
  682. static inline void
  683. rel32_unaligned(unsigned char* view, Address value)
  684. { This::template rel_unaligned<32>(view, value, CHECK_NONE); }
  685. static inline Reloc_status
  686. rel32_unaligned_check(unsigned char* view, Address value,
  687. Overflow_check check)
  688. { return This::template rel_unaligned<32>(view, value, check); }
  689. static inline void
  690. rel32(unsigned char* view,
  691. const Sized_relobj_file<size, big_endian>* object,
  692. const Symbol_value<size>* psymval)
  693. { This::template rel<32>(view, object, psymval, CHECK_NONE); }
  694. static inline Reloc_status
  695. rel32_check(unsigned char* view,
  696. const Sized_relobj_file<size, big_endian>* object,
  697. const Symbol_value<size>* psymval,
  698. Overflow_check check)
  699. { return This::template rel<32>(view, object, psymval, check); }
  700. // Like above but for relocs at unaligned addresses.
  701. static inline void
  702. rel32_unaligned(unsigned char* view,
  703. const Sized_relobj_file<size, big_endian>* object,
  704. const Symbol_value<size>* psymval)
  705. { This::template rel_unaligned<32>(view, object, psymval, CHECK_NONE); }
  706. static inline Reloc_status
  707. rel32_unaligned_check(unsigned char* view,
  708. const Sized_relobj_file<size, big_endian>* object,
  709. const Symbol_value<size>* psymval,
  710. Overflow_check check)
  711. { return This::template rel_unaligned<32>(view, object, psymval, check); }
  712. // Do a 32-bit RELA relocation with the addend in the relocation.
  713. static inline void
  714. rela32(unsigned char* view, Address value, Addendtype addend)
  715. { This::template rela<32>(view, value, addend, CHECK_NONE); }
  716. static inline Reloc_status
  717. rela32(unsigned char* view, Address value, Addendtype addend,
  718. Overflow_check check)
  719. { return This::template rela<32>(view, value, addend, check); }
  720. static inline void
  721. rela32(unsigned char* view,
  722. const Sized_relobj_file<size, big_endian>* object,
  723. const Symbol_value<size>* psymval,
  724. Addendtype addend)
  725. { This::template rela<32>(view, object, psymval, addend, CHECK_NONE); }
  726. static inline Reloc_status
  727. rela32_check(unsigned char* view,
  728. const Sized_relobj_file<size, big_endian>* object,
  729. const Symbol_value<size>* psymval,
  730. Addendtype addend,
  731. Overflow_check check)
  732. { return This::template rela<32>(view, object, psymval, addend, check); }
  733. // Do a simple 32-bit PC relative REL relocation with the addend in
  734. // the section contents.
  735. static inline void
  736. pcrel32(unsigned char* view, Address value, Address address)
  737. { This::template pcrel<32>(view, value, address, CHECK_NONE); }
  738. static inline Reloc_status
  739. pcrel32_check(unsigned char* view, Address value, Address address,
  740. Overflow_check check)
  741. { return This::template pcrel<32>(view, value, address, check); }
  742. // Unaligned version of the above.
  743. static inline void
  744. pcrel32_unaligned(unsigned char* view, Address value, Address address)
  745. { This::template pcrel_unaligned<32>(view, value, address, CHECK_NONE); }
  746. static inline Reloc_status
  747. pcrel32_unaligned_check(unsigned char* view, Address value, Address address,
  748. Overflow_check check)
  749. { return This::template pcrel_unaligned<32>(view, value, address, check); }
  750. static inline void
  751. pcrel32(unsigned char* view,
  752. const Sized_relobj_file<size, big_endian>* object,
  753. const Symbol_value<size>* psymval,
  754. Address address)
  755. { This::template pcrel<32>(view, object, psymval, address, CHECK_NONE); }
  756. static inline Reloc_status
  757. pcrel32_check(unsigned char* view,
  758. const Sized_relobj_file<size, big_endian>* object,
  759. const Symbol_value<size>* psymval,
  760. Address address,
  761. Overflow_check check)
  762. { return This::template pcrel<32>(view, object, psymval, address, check); }
  763. // Do a simple 32-bit PC relative RELA relocation with the addend in
  764. // the relocation.
  765. static inline void
  766. pcrela32(unsigned char* view, Address value, Addendtype addend,
  767. Address address)
  768. { This::template pcrela<32>(view, value, addend, address, CHECK_NONE); }
  769. static inline Reloc_status
  770. pcrela32_check(unsigned char* view, Address value, Addendtype addend,
  771. Address address, Overflow_check check)
  772. { return This::template pcrela<32>(view, value, addend, address, check); }
  773. static inline void
  774. pcrela32(unsigned char* view,
  775. const Sized_relobj_file<size, big_endian>* object,
  776. const Symbol_value<size>* psymval,
  777. Addendtype addend,
  778. Address address)
  779. { This::template pcrela<32>(view, object, psymval, addend, address,
  780. CHECK_NONE); }
  781. static inline Reloc_status
  782. pcrela32_check(unsigned char* view,
  783. const Sized_relobj_file<size, big_endian>* object,
  784. const Symbol_value<size>* psymval,
  785. Addendtype addend,
  786. Address address,
  787. Overflow_check check)
  788. { return This::template pcrela<32>(view, object, psymval, addend, address,
  789. check); }
  790. // Do a simple 64-bit REL relocation with the addend in the section
  791. // contents.
  792. static inline void
  793. rel64(unsigned char* view, Address value)
  794. { This::template rel<64>(view, value, CHECK_NONE); }
  795. static inline void
  796. rel64(unsigned char* view,
  797. const Sized_relobj_file<size, big_endian>* object,
  798. const Symbol_value<size>* psymval)
  799. { This::template rel<64>(view, object, psymval, CHECK_NONE); }
  800. // Do a 64-bit RELA relocation with the addend in the relocation.
  801. static inline void
  802. rela64(unsigned char* view, Address value, Addendtype addend)
  803. { This::template rela<64>(view, value, addend, CHECK_NONE); }
  804. static inline void
  805. rela64(unsigned char* view,
  806. const Sized_relobj_file<size, big_endian>* object,
  807. const Symbol_value<size>* psymval,
  808. Addendtype addend)
  809. { This::template rela<64>(view, object, psymval, addend, CHECK_NONE); }
  810. // Do a simple 64-bit PC relative REL relocation with the addend in
  811. // the section contents.
  812. static inline void
  813. pcrel64(unsigned char* view, Address value, Address address)
  814. { This::template pcrel<64>(view, value, address, CHECK_NONE); }
  815. static inline void
  816. pcrel64(unsigned char* view,
  817. const Sized_relobj_file<size, big_endian>* object,
  818. const Symbol_value<size>* psymval,
  819. Address address)
  820. { This::template pcrel<64>(view, object, psymval, address, CHECK_NONE); }
  821. // Do a simple 64-bit PC relative RELA relocation with the addend in
  822. // the relocation.
  823. static inline void
  824. pcrela64(unsigned char* view, Address value, Addendtype addend,
  825. Address address)
  826. { This::template pcrela<64>(view, value, addend, address, CHECK_NONE); }
  827. static inline void
  828. pcrela64(unsigned char* view,
  829. const Sized_relobj_file<size, big_endian>* object,
  830. const Symbol_value<size>* psymval,
  831. Addendtype addend,
  832. Address address)
  833. { This::template pcrela<64>(view, object, psymval, addend, address,
  834. CHECK_NONE); }
  835. };
  836. // Convenience class for min and max values of a given BITS length.
  837. template<int bits>
  838. class Limits
  839. {
  840. public:
  841. static const uint64_t MAX_UNSIGNED = (1ULL << bits) - 1;
  842. static const int64_t MAX_SIGNED = MAX_UNSIGNED >> 1;
  843. static const int64_t MIN_SIGNED = -MAX_SIGNED - 1;
  844. };
  845. template<>
  846. class Limits<64>
  847. {
  848. public:
  849. static const uint64_t MAX_UNSIGNED = ~0ULL;
  850. static const int64_t MAX_SIGNED = MAX_UNSIGNED >> 1;
  851. static const int64_t MIN_SIGNED = -MAX_SIGNED - 1;
  852. };
  853. // Integer manipulation functions used by various targets when
  854. // performing relocations.
  855. template<int bits>
  856. class Bits
  857. {
  858. public:
  859. // Sign extend an n-bit unsigned integer stored in a uint32_t into
  860. // an int32_t. BITS must be between 1 and 32.
  861. static inline int32_t
  862. sign_extend32(uint32_t val)
  863. {
  864. gold_assert(bits > 0 && bits <= 32);
  865. if (bits == 32)
  866. return static_cast<int32_t>(val);
  867. uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
  868. val &= mask;
  869. uint32_t top_bit = 1U << (bits - 1);
  870. int32_t as_signed = static_cast<int32_t>(val);
  871. if ((val & top_bit) != 0)
  872. as_signed -= static_cast<int32_t>(top_bit * 2);
  873. return as_signed;
  874. }
  875. // Return true if VAL (stored in a uint32_t) has overflowed a signed
  876. // value with BITS bits.
  877. static inline bool
  878. has_overflow32(uint32_t val)
  879. {
  880. gold_assert(bits > 0 && bits <= 32);
  881. if (bits == 32)
  882. return false;
  883. const int32_t max = static_cast<int32_t>(Limits<bits>::MAX_SIGNED);
  884. const int32_t min = static_cast<int32_t>(Limits<bits>::MIN_SIGNED);
  885. int32_t as_signed = static_cast<int32_t>(val);
  886. return as_signed > max || as_signed < min;
  887. }
  888. // Return true if VAL (stored in a uint32_t) has overflowed an unsigned
  889. // value with BITS bits.
  890. static inline bool
  891. has_unsigned_overflow32(uint32_t val)
  892. {
  893. gold_assert(bits > 0 && bits <= 32);
  894. if (bits == 32)
  895. return false;
  896. const uint32_t max = static_cast<uint32_t>(Limits<bits>::MAX_UNSIGNED);
  897. return val > max;
  898. }
  899. // Return true if VAL (stored in a uint32_t) has overflowed both a
  900. // signed and an unsigned value. E.g.,
  901. // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
  902. // 255.
  903. static inline bool
  904. has_signed_unsigned_overflow32(uint32_t val)
  905. {
  906. gold_assert(bits > 0 && bits <= 32);
  907. if (bits == 32)
  908. return false;
  909. const int32_t max = static_cast<int32_t>(Limits<bits>::MAX_UNSIGNED);
  910. const int32_t min = static_cast<int32_t>(Limits<bits>::MIN_SIGNED);
  911. int32_t as_signed = static_cast<int32_t>(val);
  912. return as_signed > max || as_signed < min;
  913. }
  914. // Select bits from A and B using bits in MASK. For each n in
  915. // [0..31], the n-th bit in the result is chosen from the n-th bits
  916. // of A and B. A zero selects A and a one selects B.
  917. static inline uint32_t
  918. bit_select32(uint32_t a, uint32_t b, uint32_t mask)
  919. { return (a & ~mask) | (b & mask); }
  920. // Sign extend an n-bit unsigned integer stored in a uint64_t into
  921. // an int64_t. BITS must be between 1 and 64.
  922. static inline int64_t
  923. sign_extend(uint64_t val)
  924. {
  925. gold_assert(bits > 0 && bits <= 64);
  926. if (bits == 64)
  927. return static_cast<int64_t>(val);
  928. uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
  929. val &= mask;
  930. uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
  931. int64_t as_signed = static_cast<int64_t>(val);
  932. if ((val & top_bit) != 0)
  933. as_signed -= static_cast<int64_t>(top_bit * 2);
  934. return as_signed;
  935. }
  936. // Return true if VAL (stored in a uint64_t) has overflowed a signed
  937. // value with BITS bits.
  938. static inline bool
  939. has_overflow(uint64_t val)
  940. {
  941. gold_assert(bits > 0 && bits <= 64);
  942. if (bits == 64)
  943. return false;
  944. const int64_t max = Limits<bits>::MAX_SIGNED;
  945. const int64_t min = Limits<bits>::MIN_SIGNED;
  946. int64_t as_signed = static_cast<int64_t>(val);
  947. return as_signed > max || as_signed < min;
  948. }
  949. // Return true if VAL (stored in a uint64_t) has overflowed an unsigned
  950. // value with BITS bits.
  951. static inline bool
  952. has_unsigned_overflow(uint64_t val)
  953. {
  954. gold_assert(bits > 0 && bits <= 64);
  955. if (bits == 64)
  956. return false;
  957. const uint64_t max = Limits<bits>::MAX_UNSIGNED;
  958. return val > max;
  959. }
  960. // Return true if VAL (stored in a uint64_t) has overflowed both a
  961. // signed and an unsigned value. E.g.,
  962. // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
  963. // 255.
  964. static inline bool
  965. has_signed_unsigned_overflow64(uint64_t val)
  966. {
  967. gold_assert(bits > 0 && bits <= 64);
  968. if (bits == 64)
  969. return false;
  970. const int64_t max = static_cast<int64_t>(Limits<bits>::MAX_UNSIGNED);
  971. const int64_t min = Limits<bits>::MIN_SIGNED;
  972. int64_t as_signed = static_cast<int64_t>(val);
  973. return as_signed > max || as_signed < min;
  974. }
  975. // Select bits from A and B using bits in MASK. For each n in
  976. // [0..31], the n-th bit in the result is chosen from the n-th bits
  977. // of A and B. A zero selects A and a one selects B.
  978. static inline uint64_t
  979. bit_select64(uint64_t a, uint64_t b, uint64_t mask)
  980. { return (a & ~mask) | (b & mask); }
  981. };
  982. // Track relocations while reading a section. This lets you ask for
  983. // the relocation at a certain offset, and see how relocs occur
  984. // between points of interest.
  985. template<int size, bool big_endian>
  986. class Track_relocs
  987. {
  988. public:
  989. Track_relocs()
  990. : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
  991. { }
  992. // Initialize the Track_relocs object. OBJECT is the object holding
  993. // the reloc section, RELOC_SHNDX is the section index of the reloc
  994. // section, and RELOC_TYPE is the type of the reloc section
  995. // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
  996. // something went wrong.
  997. bool
  998. initialize(Object* object, unsigned int reloc_shndx,
  999. unsigned int reloc_type);
  1000. // Return the offset in the data section to which the next reloc
  1001. // applies. This returns -1 if there is no next reloc.
  1002. off_t
  1003. next_offset() const;
  1004. // Return the symbol index of the next reloc. This returns -1U if
  1005. // there is no next reloc.
  1006. unsigned int
  1007. next_symndx() const;
  1008. // Return the addend of the next reloc. This returns 0 if there is
  1009. // no next reloc.
  1010. uint64_t
  1011. next_addend() const;
  1012. // Advance to OFFSET within the data section, and return the number
  1013. // of relocs which would be skipped, excluding r_info==0 relocs.
  1014. int
  1015. advance(off_t offset);
  1016. // Checkpoint the current position in the reloc section.
  1017. section_size_type
  1018. checkpoint() const
  1019. { return this->pos_; }
  1020. // Reset the position to CHECKPOINT.
  1021. void
  1022. reset(section_size_type checkpoint)
  1023. { this->pos_ = checkpoint; }
  1024. private:
  1025. // The contents of the input object's reloc section.
  1026. const unsigned char* prelocs_;
  1027. // The length of the reloc section.
  1028. section_size_type len_;
  1029. // Our current position in the reloc section.
  1030. section_size_type pos_;
  1031. // The size of the relocs in the section.
  1032. int reloc_size_;
  1033. };
  1034. } // End namespace gold.
  1035. #endif // !defined(GOLD_RELOC_H)