reloc.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // reloc.h -- relocate input files for gold -*- C++ -*-
  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. #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. // Return the number of relocations to create in the output file.
  243. size_t
  244. output_reloc_count() const
  245. { return this->output_reloc_count_; }
  246. private:
  247. typedef std::vector<unsigned char> Reloc_strategies;
  248. // The strategies for the input reloc. There is one entry in this
  249. // vector for each relocation in the input section.
  250. Reloc_strategies reloc_strategies_;
  251. // The number of relocations to be created in the output file.
  252. size_t output_reloc_count_;
  253. // The output data structure associated with this relocation.
  254. Output_data* posd_;
  255. };
  256. // Standard relocation routines which are used on many targets. Here
  257. // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
  258. template<int size, bool big_endian>
  259. class Relocate_functions
  260. {
  261. private:
  262. // Do a simple relocation with the addend in the section contents.
  263. // VALSIZE is the size of the value.
  264. template<int valsize>
  265. static inline void
  266. rel(unsigned char* view,
  267. typename elfcpp::Swap<valsize, big_endian>::Valtype value)
  268. {
  269. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  270. Valtype* wv = reinterpret_cast<Valtype*>(view);
  271. Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
  272. elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
  273. }
  274. // Like the above but for relocs at unaligned addresses.
  275. template<int valsize>
  276. static inline void
  277. rel_unaligned(unsigned char* view,
  278. typename elfcpp::Swap<valsize, big_endian>::Valtype value)
  279. {
  280. typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
  281. Valtype;
  282. Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
  283. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x + value);
  284. }
  285. // Do a simple relocation using a Symbol_value with the addend in
  286. // the section contents. VALSIZE is the size of the value to
  287. // relocate.
  288. template<int valsize>
  289. static inline void
  290. rel(unsigned char* view,
  291. const Sized_relobj_file<size, big_endian>* object,
  292. const Symbol_value<size>* psymval)
  293. {
  294. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  295. Valtype* wv = reinterpret_cast<Valtype*>(view);
  296. Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
  297. x = psymval->value(object, x);
  298. elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
  299. }
  300. // Like the above but for relocs at unaligned addresses.
  301. template<int valsize>
  302. static inline void
  303. rel_unaligned(unsigned char* view,
  304. const Sized_relobj_file<size, big_endian>* object,
  305. const Symbol_value<size>* psymval)
  306. {
  307. typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
  308. Valtype;
  309. Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
  310. x = psymval->value(object, x);
  311. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x);
  312. }
  313. // Do a simple relocation with the addend in the relocation.
  314. // VALSIZE is the size of the value.
  315. template<int valsize>
  316. static inline void
  317. rela(unsigned char* view,
  318. typename elfcpp::Swap<valsize, big_endian>::Valtype value,
  319. typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
  320. {
  321. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  322. Valtype* wv = reinterpret_cast<Valtype*>(view);
  323. elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
  324. }
  325. // Do a simple relocation using a symbol value with the addend in
  326. // the relocation. VALSIZE is the size of the value.
  327. template<int valsize>
  328. static inline void
  329. rela(unsigned char* view,
  330. const Sized_relobj_file<size, big_endian>* object,
  331. const Symbol_value<size>* psymval,
  332. typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
  333. {
  334. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  335. Valtype* wv = reinterpret_cast<Valtype*>(view);
  336. Valtype x = psymval->value(object, addend);
  337. elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
  338. }
  339. // Do a simple PC relative relocation with the addend in the section
  340. // contents. VALSIZE is the size of the value.
  341. template<int valsize>
  342. static inline void
  343. pcrel(unsigned char* view,
  344. typename elfcpp::Swap<valsize, big_endian>::Valtype value,
  345. typename elfcpp::Elf_types<size>::Elf_Addr address)
  346. {
  347. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  348. Valtype* wv = reinterpret_cast<Valtype*>(view);
  349. Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
  350. elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
  351. }
  352. // Like the above but for relocs at unaligned addresses.
  353. template<int valsize>
  354. static inline void
  355. pcrel_unaligned(unsigned char* view,
  356. typename elfcpp::Swap<valsize, big_endian>::Valtype value,
  357. typename elfcpp::Elf_types<size>::Elf_Addr address)
  358. {
  359. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  360. Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
  361. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
  362. x + value - address);
  363. }
  364. // Do a simple PC relative relocation with a Symbol_value with the
  365. // addend in the section contents. VALSIZE is the size of the
  366. // value.
  367. template<int valsize>
  368. static inline void
  369. pcrel(unsigned char* view,
  370. const Sized_relobj_file<size, big_endian>* object,
  371. const Symbol_value<size>* psymval,
  372. typename elfcpp::Elf_types<size>::Elf_Addr address)
  373. {
  374. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  375. Valtype* wv = reinterpret_cast<Valtype*>(view);
  376. Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
  377. x = psymval->value(object, x);
  378. elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
  379. }
  380. // Do a simple PC relative relocation with the addend in the
  381. // relocation. VALSIZE is the size of the value.
  382. template<int valsize>
  383. static inline void
  384. pcrela(unsigned char* view,
  385. typename elfcpp::Swap<valsize, big_endian>::Valtype value,
  386. typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
  387. typename elfcpp::Elf_types<size>::Elf_Addr address)
  388. {
  389. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  390. Valtype* wv = reinterpret_cast<Valtype*>(view);
  391. elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
  392. }
  393. // Do a simple PC relative relocation with a Symbol_value with the
  394. // addend in the relocation. VALSIZE is the size of the value.
  395. template<int valsize>
  396. static inline void
  397. pcrela(unsigned char* view,
  398. const Sized_relobj_file<size, big_endian>* object,
  399. const Symbol_value<size>* psymval,
  400. typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
  401. typename elfcpp::Elf_types<size>::Elf_Addr address)
  402. {
  403. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  404. Valtype* wv = reinterpret_cast<Valtype*>(view);
  405. Valtype x = psymval->value(object, addend);
  406. elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
  407. }
  408. typedef Relocate_functions<size, big_endian> This;
  409. public:
  410. // Do a simple 8-bit REL relocation with the addend in the section
  411. // contents.
  412. static inline void
  413. rel8(unsigned char* view, unsigned char value)
  414. { This::template rel<8>(view, value); }
  415. static inline void
  416. rel8(unsigned char* view,
  417. const Sized_relobj_file<size, big_endian>* object,
  418. const Symbol_value<size>* psymval)
  419. { This::template rel<8>(view, object, psymval); }
  420. // Do an 8-bit RELA relocation with the addend in the relocation.
  421. static inline void
  422. rela8(unsigned char* view, unsigned char value, unsigned char addend)
  423. { This::template rela<8>(view, value, addend); }
  424. static inline void
  425. rela8(unsigned char* view,
  426. const Sized_relobj_file<size, big_endian>* object,
  427. const Symbol_value<size>* psymval,
  428. unsigned char addend)
  429. { This::template rela<8>(view, object, psymval, addend); }
  430. // Do a simple 8-bit PC relative relocation with the addend in the
  431. // section contents.
  432. static inline void
  433. pcrel8(unsigned char* view, unsigned char value,
  434. typename elfcpp::Elf_types<size>::Elf_Addr address)
  435. { This::template pcrel<8>(view, value, address); }
  436. static inline void
  437. pcrel8(unsigned char* view,
  438. const Sized_relobj_file<size, big_endian>* object,
  439. const Symbol_value<size>* psymval,
  440. typename elfcpp::Elf_types<size>::Elf_Addr address)
  441. { This::template pcrel<8>(view, object, psymval, address); }
  442. // Do a simple 8-bit PC relative RELA relocation with the addend in
  443. // the reloc.
  444. static inline void
  445. pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
  446. typename elfcpp::Elf_types<size>::Elf_Addr address)
  447. { This::template pcrela<8>(view, value, addend, address); }
  448. static inline void
  449. pcrela8(unsigned char* view,
  450. const Sized_relobj_file<size, big_endian>* object,
  451. const Symbol_value<size>* psymval,
  452. unsigned char addend,
  453. typename elfcpp::Elf_types<size>::Elf_Addr address)
  454. { This::template pcrela<8>(view, object, psymval, addend, address); }
  455. // Do a simple 16-bit REL relocation with the addend in the section
  456. // contents.
  457. static inline void
  458. rel16(unsigned char* view, elfcpp::Elf_Half value)
  459. { This::template rel<16>(view, value); }
  460. static inline void
  461. rel16(unsigned char* view,
  462. const Sized_relobj_file<size, big_endian>* object,
  463. const Symbol_value<size>* psymval)
  464. { This::template rel<16>(view, object, psymval); }
  465. // Do an 16-bit RELA relocation with the addend in the relocation.
  466. static inline void
  467. rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
  468. { This::template rela<16>(view, value, addend); }
  469. static inline void
  470. rela16(unsigned char* view,
  471. const Sized_relobj_file<size, big_endian>* object,
  472. const Symbol_value<size>* psymval,
  473. elfcpp::Elf_Half addend)
  474. { This::template rela<16>(view, object, psymval, addend); }
  475. // Do a simple 16-bit PC relative REL relocation with the addend in
  476. // the section contents.
  477. static inline void
  478. pcrel16(unsigned char* view, elfcpp::Elf_Half value,
  479. typename elfcpp::Elf_types<size>::Elf_Addr address)
  480. { This::template pcrel<16>(view, value, address); }
  481. static inline void
  482. pcrel16(unsigned char* view,
  483. const Sized_relobj_file<size, big_endian>* object,
  484. const Symbol_value<size>* psymval,
  485. typename elfcpp::Elf_types<size>::Elf_Addr address)
  486. { This::template pcrel<16>(view, object, psymval, address); }
  487. // Do a simple 16-bit PC relative RELA relocation with the addend in
  488. // the reloc.
  489. static inline void
  490. pcrela16(unsigned char* view, elfcpp::Elf_Half value,
  491. elfcpp::Elf_Half addend,
  492. typename elfcpp::Elf_types<size>::Elf_Addr address)
  493. { This::template pcrela<16>(view, value, addend, address); }
  494. static inline void
  495. pcrela16(unsigned char* view,
  496. const Sized_relobj_file<size, big_endian>* object,
  497. const Symbol_value<size>* psymval,
  498. elfcpp::Elf_Half addend,
  499. typename elfcpp::Elf_types<size>::Elf_Addr address)
  500. { This::template pcrela<16>(view, object, psymval, addend, address); }
  501. // Do a simple 32-bit REL relocation with the addend in the section
  502. // contents.
  503. static inline void
  504. rel32(unsigned char* view, elfcpp::Elf_Word value)
  505. { This::template rel<32>(view, value); }
  506. // Like above but for relocs at unaligned addresses.
  507. static inline void
  508. rel32_unaligned(unsigned char* view, elfcpp::Elf_Word value)
  509. { This::template rel_unaligned<32>(view, value); }
  510. static inline void
  511. rel32(unsigned char* view,
  512. const Sized_relobj_file<size, big_endian>* object,
  513. const Symbol_value<size>* psymval)
  514. { This::template rel<32>(view, object, psymval); }
  515. // Like above but for relocs at unaligned addresses.
  516. static inline void
  517. rel32_unaligned(unsigned char* view,
  518. const Sized_relobj_file<size, big_endian>* object,
  519. const Symbol_value<size>* psymval)
  520. { This::template rel_unaligned<32>(view, object, psymval); }
  521. // Do an 32-bit RELA relocation with the addend in the relocation.
  522. static inline void
  523. rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
  524. { This::template rela<32>(view, value, addend); }
  525. static inline void
  526. rela32(unsigned char* view,
  527. const Sized_relobj_file<size, big_endian>* object,
  528. const Symbol_value<size>* psymval,
  529. elfcpp::Elf_Word addend)
  530. { This::template rela<32>(view, object, psymval, addend); }
  531. // Do a simple 32-bit PC relative REL relocation with the addend in
  532. // the section contents.
  533. static inline void
  534. pcrel32(unsigned char* view, elfcpp::Elf_Word value,
  535. typename elfcpp::Elf_types<size>::Elf_Addr address)
  536. { This::template pcrel<32>(view, value, address); }
  537. // Unaligned version of the above.
  538. static inline void
  539. pcrel32_unaligned(unsigned char* view, elfcpp::Elf_Word value,
  540. typename elfcpp::Elf_types<size>::Elf_Addr address)
  541. { This::template pcrel_unaligned<32>(view, value, address); }
  542. static inline void
  543. pcrel32(unsigned char* view,
  544. const Sized_relobj_file<size, big_endian>* object,
  545. const Symbol_value<size>* psymval,
  546. typename elfcpp::Elf_types<size>::Elf_Addr address)
  547. { This::template pcrel<32>(view, object, psymval, address); }
  548. // Do a simple 32-bit PC relative RELA relocation with the addend in
  549. // the relocation.
  550. static inline void
  551. pcrela32(unsigned char* view, elfcpp::Elf_Word value,
  552. elfcpp::Elf_Word addend,
  553. typename elfcpp::Elf_types<size>::Elf_Addr address)
  554. { This::template pcrela<32>(view, value, addend, address); }
  555. static inline void
  556. pcrela32(unsigned char* view,
  557. const Sized_relobj_file<size, big_endian>* object,
  558. const Symbol_value<size>* psymval,
  559. elfcpp::Elf_Word addend,
  560. typename elfcpp::Elf_types<size>::Elf_Addr address)
  561. { This::template pcrela<32>(view, object, psymval, addend, address); }
  562. // Do a simple 64-bit REL relocation with the addend in the section
  563. // contents.
  564. static inline void
  565. rel64(unsigned char* view, elfcpp::Elf_Xword value)
  566. { This::template rel<64>(view, value); }
  567. static inline void
  568. rel64(unsigned char* view,
  569. const Sized_relobj_file<size, big_endian>* object,
  570. const Symbol_value<size>* psymval)
  571. { This::template rel<64>(view, object, psymval); }
  572. // Do a 64-bit RELA relocation with the addend in the relocation.
  573. static inline void
  574. rela64(unsigned char* view, elfcpp::Elf_Xword value,
  575. elfcpp::Elf_Xword addend)
  576. { This::template rela<64>(view, value, addend); }
  577. static inline void
  578. rela64(unsigned char* view,
  579. const Sized_relobj_file<size, big_endian>* object,
  580. const Symbol_value<size>* psymval,
  581. elfcpp::Elf_Xword addend)
  582. { This::template rela<64>(view, object, psymval, addend); }
  583. // Do a simple 64-bit PC relative REL relocation with the addend in
  584. // the section contents.
  585. static inline void
  586. pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
  587. typename elfcpp::Elf_types<size>::Elf_Addr address)
  588. { This::template pcrel<64>(view, value, address); }
  589. static inline void
  590. pcrel64(unsigned char* view,
  591. const Sized_relobj_file<size, big_endian>* object,
  592. const Symbol_value<size>* psymval,
  593. typename elfcpp::Elf_types<size>::Elf_Addr address)
  594. { This::template pcrel<64>(view, object, psymval, address); }
  595. // Do a simple 64-bit PC relative RELA relocation with the addend in
  596. // the relocation.
  597. static inline void
  598. pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
  599. elfcpp::Elf_Xword addend,
  600. typename elfcpp::Elf_types<size>::Elf_Addr address)
  601. { This::template pcrela<64>(view, value, addend, address); }
  602. static inline void
  603. pcrela64(unsigned char* view,
  604. const Sized_relobj_file<size, big_endian>* object,
  605. const Symbol_value<size>* psymval,
  606. elfcpp::Elf_Xword addend,
  607. typename elfcpp::Elf_types<size>::Elf_Addr address)
  608. { This::template pcrela<64>(view, object, psymval, addend, address); }
  609. };
  610. // Integer manipulation functions used by various targets when
  611. // performing relocations.
  612. template<int bits>
  613. class Bits
  614. {
  615. public:
  616. // Sign extend an n-bit unsigned integer stored in a uint32_t into
  617. // an int32_t. BITS must be between 1 and 32.
  618. static inline int32_t
  619. sign_extend32(uint32_t val)
  620. {
  621. gold_assert(bits > 0 && bits <= 32);
  622. if (bits == 32)
  623. return static_cast<int32_t>(val);
  624. uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
  625. val &= mask;
  626. uint32_t top_bit = 1U << (bits - 1);
  627. int32_t as_signed = static_cast<int32_t>(val);
  628. if ((val & top_bit) != 0)
  629. as_signed -= static_cast<int32_t>(top_bit * 2);
  630. return as_signed;
  631. }
  632. // Return true if VAL (stored in a uint32_t) has overflowed a signed
  633. // value with BITS bits.
  634. static inline bool
  635. has_overflow32(uint32_t val)
  636. {
  637. gold_assert(bits > 0 && bits <= 32);
  638. if (bits == 32)
  639. return false;
  640. int32_t max = (1 << (bits - 1)) - 1;
  641. int32_t min = -(1 << (bits - 1));
  642. int32_t as_signed = static_cast<int32_t>(val);
  643. return as_signed > max || as_signed < min;
  644. }
  645. // Return true if VAL (stored in a uint32_t) has overflowed both a
  646. // signed and an unsigned value. E.g.,
  647. // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
  648. // 255.
  649. static inline bool
  650. has_signed_unsigned_overflow32(uint32_t val)
  651. {
  652. gold_assert(bits > 0 && bits <= 32);
  653. if (bits == 32)
  654. return false;
  655. int32_t max = static_cast<int32_t>((1U << bits) - 1);
  656. int32_t min = -(1 << (bits - 1));
  657. int32_t as_signed = static_cast<int32_t>(val);
  658. return as_signed > max || as_signed < min;
  659. }
  660. // Select bits from A and B using bits in MASK. For each n in
  661. // [0..31], the n-th bit in the result is chosen from the n-th bits
  662. // of A and B. A zero selects A and a one selects B.
  663. static inline uint32_t
  664. bit_select32(uint32_t a, uint32_t b, uint32_t mask)
  665. { return (a & ~mask) | (b & mask); }
  666. // Sign extend an n-bit unsigned integer stored in a uint64_t into
  667. // an int64_t. BITS must be between 1 and 64.
  668. static inline int64_t
  669. sign_extend(uint64_t val)
  670. {
  671. gold_assert(bits > 0 && bits <= 64);
  672. if (bits == 64)
  673. return static_cast<int64_t>(val);
  674. uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
  675. val &= mask;
  676. uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
  677. int64_t as_signed = static_cast<int64_t>(val);
  678. if ((val & top_bit) != 0)
  679. as_signed -= static_cast<int64_t>(top_bit * 2);
  680. return as_signed;
  681. }
  682. // Return true if VAL (stored in a uint64_t) has overflowed a signed
  683. // value with BITS bits.
  684. static inline bool
  685. has_overflow(uint64_t val)
  686. {
  687. gold_assert(bits > 0 && bits <= 64);
  688. if (bits == 64)
  689. return false;
  690. int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1;
  691. int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
  692. int64_t as_signed = static_cast<int64_t>(val);
  693. return as_signed > max || as_signed < min;
  694. }
  695. // Return true if VAL (stored in a uint64_t) has overflowed both a
  696. // signed and an unsigned value. E.g.,
  697. // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
  698. // 255.
  699. static inline bool
  700. has_signed_unsigned_overflow64(uint64_t val)
  701. {
  702. gold_assert(bits > 0 && bits <= 64);
  703. if (bits == 64)
  704. return false;
  705. int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
  706. int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
  707. int64_t as_signed = static_cast<int64_t>(val);
  708. return as_signed > max || as_signed < min;
  709. }
  710. // Select bits from A and B using bits in MASK. For each n in
  711. // [0..31], the n-th bit in the result is chosen from the n-th bits
  712. // of A and B. A zero selects A and a one selects B.
  713. static inline uint64_t
  714. bit_select64(uint64_t a, uint64_t b, uint64_t mask)
  715. { return (a & ~mask) | (b & mask); }
  716. };
  717. // Track relocations while reading a section. This lets you ask for
  718. // the relocation at a certain offset, and see how relocs occur
  719. // between points of interest.
  720. template<int size, bool big_endian>
  721. class Track_relocs
  722. {
  723. public:
  724. Track_relocs()
  725. : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
  726. { }
  727. // Initialize the Track_relocs object. OBJECT is the object holding
  728. // the reloc section, RELOC_SHNDX is the section index of the reloc
  729. // section, and RELOC_TYPE is the type of the reloc section
  730. // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
  731. // something went wrong.
  732. bool
  733. initialize(Object* object, unsigned int reloc_shndx,
  734. unsigned int reloc_type);
  735. // Return the offset in the data section to which the next reloc
  736. // applies. This returns -1 if there is no next reloc.
  737. off_t
  738. next_offset() const;
  739. // Return the symbol index of the next reloc. This returns -1U if
  740. // there is no next reloc.
  741. unsigned int
  742. next_symndx() const;
  743. // Return the addend of the next reloc. This returns 0 if there is
  744. // no next reloc.
  745. uint64_t
  746. next_addend() const;
  747. // Advance to OFFSET within the data section, and return the number
  748. // of relocs which would be skipped.
  749. int
  750. advance(off_t offset);
  751. // Checkpoint the current position in the reloc section.
  752. section_size_type
  753. checkpoint() const
  754. { return this->pos_; }
  755. // Reset the position to CHECKPOINT.
  756. void
  757. reset(section_size_type checkpoint)
  758. { this->pos_ = checkpoint; }
  759. private:
  760. // The contents of the input object's reloc section.
  761. const unsigned char* prelocs_;
  762. // The length of the reloc section.
  763. section_size_type len_;
  764. // Our current position in the reloc section.
  765. section_size_type pos_;
  766. // The size of the relocs in the section.
  767. int reloc_size_;
  768. };
  769. } // End namespace gold.
  770. #endif // !defined(GOLD_RELOC_H)