incremental.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. // inremental.h -- incremental linking support for gold -*- C++ -*-
  2. // Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. // Written by Mikolaj Zalewski <mikolajz@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_INCREMENTAL_H
  18. #define GOLD_INCREMENTAL_H
  19. #include <map>
  20. #include <vector>
  21. #include "elfcpp_file.h"
  22. #include "stringpool.h"
  23. #include "workqueue.h"
  24. #include "fileread.h"
  25. #include "output.h"
  26. #include "archive.h"
  27. namespace gold
  28. {
  29. class Input_argument;
  30. class Incremental_inputs_checker;
  31. class Incremental_script_entry;
  32. class Incremental_object_entry;
  33. class Incremental_dynobj_entry;
  34. class Incremental_archive_entry;
  35. class Incremental_inputs;
  36. class Incremental_binary;
  37. class Incremental_library;
  38. class Object;
  39. // Incremental input type as stored in .gnu_incremental_inputs.
  40. enum Incremental_input_type
  41. {
  42. INCREMENTAL_INPUT_OBJECT = 1,
  43. INCREMENTAL_INPUT_ARCHIVE_MEMBER = 2,
  44. INCREMENTAL_INPUT_ARCHIVE = 3,
  45. INCREMENTAL_INPUT_SHARED_LIBRARY = 4,
  46. INCREMENTAL_INPUT_SCRIPT = 5
  47. };
  48. // Incremental input file flags.
  49. // The input file type is stored in the lower eight bits.
  50. enum Incremental_input_flags
  51. {
  52. INCREMENTAL_INPUT_IN_SYSTEM_DIR = 0x8000,
  53. INCREMENTAL_INPUT_AS_NEEDED = 0x4000
  54. };
  55. // Symbol flags for the incremental symbol table.
  56. // These flags are stored in the top two bits of
  57. // the symbol index field.
  58. enum Incremental_shlib_symbol_flags
  59. {
  60. // Symbol is defined in this library.
  61. INCREMENTAL_SHLIB_SYM_DEF = 2,
  62. // Symbol is defined in this library, with a COPY relocation.
  63. INCREMENTAL_SHLIB_SYM_COPY = 3
  64. };
  65. static const int INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT = 30;
  66. // Return TRUE if a section of type SH_TYPE can be updated in place
  67. // during an incremental update.
  68. bool
  69. can_incremental_update(unsigned int sh_type);
  70. // Create an Incremental_binary object for FILE. Returns NULL is this is not
  71. // possible, e.g. FILE is not an ELF file or has an unsupported target.
  72. Incremental_binary*
  73. open_incremental_binary(Output_file* file);
  74. // Base class for recording each input file.
  75. class Incremental_input_entry
  76. {
  77. public:
  78. Incremental_input_entry(Stringpool::Key filename_key, unsigned int arg_serial,
  79. Timespec mtime)
  80. : filename_key_(filename_key), file_index_(0), offset_(0), info_offset_(0),
  81. arg_serial_(arg_serial), mtime_(mtime), is_in_system_directory_(false),
  82. as_needed_(false)
  83. { }
  84. virtual
  85. ~Incremental_input_entry()
  86. { }
  87. // Return the type of input file.
  88. Incremental_input_type
  89. type() const
  90. { return this->do_type(); }
  91. // Set the index and section offset of this input file entry.
  92. void
  93. set_offset(unsigned int file_index, unsigned int offset)
  94. {
  95. this->file_index_ = file_index;
  96. this->offset_ = offset;
  97. }
  98. // Set the section offset of the supplemental information for this entry.
  99. void
  100. set_info_offset(unsigned int info_offset)
  101. { this->info_offset_ = info_offset; }
  102. // Get the index of this input file entry.
  103. unsigned int
  104. get_file_index() const
  105. { return this->file_index_; }
  106. // Get the section offset of this input file entry.
  107. unsigned int
  108. get_offset() const
  109. { return this->offset_; }
  110. // Get the section offset of the supplemental information for this entry.
  111. unsigned int
  112. get_info_offset() const
  113. { return this->info_offset_; }
  114. // Get the stringpool key for the input filename.
  115. Stringpool::Key
  116. get_filename_key() const
  117. { return this->filename_key_; }
  118. // Get the serial number of the input file.
  119. unsigned int
  120. arg_serial() const
  121. { return this->arg_serial_; }
  122. // Get the modification time of the input file.
  123. const Timespec&
  124. get_mtime() const
  125. { return this->mtime_; }
  126. // Record that the file was found in a system directory.
  127. void
  128. set_is_in_system_directory()
  129. { this->is_in_system_directory_ = true; }
  130. // Return TRUE if the file was found in a system directory.
  131. bool
  132. is_in_system_directory() const
  133. { return this->is_in_system_directory_; }
  134. // Record that the file was linked with --as-needed.
  135. void
  136. set_as_needed()
  137. { this->as_needed_ = true; }
  138. // Return TRUE if the file was linked with --as-needed.
  139. bool
  140. as_needed() const
  141. { return this->as_needed_; }
  142. // Return a pointer to the derived Incremental_script_entry object.
  143. // Return NULL for input entries that are not script files.
  144. Incremental_script_entry*
  145. script_entry()
  146. { return this->do_script_entry(); }
  147. // Return a pointer to the derived Incremental_object_entry object.
  148. // Return NULL for input entries that are not object files.
  149. Incremental_object_entry*
  150. object_entry()
  151. { return this->do_object_entry(); }
  152. // Return a pointer to the derived Incremental_dynobj_entry object.
  153. // Return NULL for input entries that are not shared object files.
  154. Incremental_dynobj_entry*
  155. dynobj_entry()
  156. { return this->do_dynobj_entry(); }
  157. // Return a pointer to the derived Incremental_archive_entry object.
  158. // Return NULL for input entries that are not archive files.
  159. Incremental_archive_entry*
  160. archive_entry()
  161. { return this->do_archive_entry(); }
  162. protected:
  163. // Return the type of input file.
  164. virtual Incremental_input_type
  165. do_type() const = 0;
  166. // Return a pointer to the derived Incremental_script_entry object.
  167. // Return NULL for input entries that are not script files.
  168. virtual Incremental_script_entry*
  169. do_script_entry()
  170. { return NULL; }
  171. // Return a pointer to the derived Incremental_object_entry object.
  172. // Return NULL for input entries that are not object files.
  173. virtual Incremental_object_entry*
  174. do_object_entry()
  175. { return NULL; }
  176. // Return a pointer to the derived Incremental_dynobj_entry object.
  177. // Return NULL for input entries that are not shared object files.
  178. virtual Incremental_dynobj_entry*
  179. do_dynobj_entry()
  180. { return NULL; }
  181. // Return a pointer to the derived Incremental_archive_entry object.
  182. // Return NULL for input entries that are not archive files.
  183. virtual Incremental_archive_entry*
  184. do_archive_entry()
  185. { return NULL; }
  186. private:
  187. // Key of the filename string in the section stringtable.
  188. Stringpool::Key filename_key_;
  189. // Index of the entry in the output section.
  190. unsigned int file_index_;
  191. // Offset of the entry in the output section.
  192. unsigned int offset_;
  193. // Offset of the extra information in the output section.
  194. unsigned int info_offset_;
  195. // Serial number of the file in the argument list.
  196. unsigned int arg_serial_;
  197. // Last modification time of the file.
  198. Timespec mtime_;
  199. // TRUE if the file was found in a system directory.
  200. bool is_in_system_directory_;
  201. // TRUE if the file was linked with --as-needed.
  202. bool as_needed_;
  203. };
  204. // Information about a script input that will persist during the whole linker
  205. // run. Needed only during an incremental build to retrieve the input files
  206. // added by this script.
  207. class Script_info
  208. {
  209. public:
  210. Script_info(const std::string& filename)
  211. : filename_(filename), input_file_index_(0),
  212. incremental_script_entry_(NULL)
  213. { }
  214. Script_info(const std::string& filename, unsigned int input_file_index)
  215. : filename_(filename), input_file_index_(input_file_index),
  216. incremental_script_entry_(NULL)
  217. { }
  218. // Store a pointer to the incremental information for this script.
  219. void
  220. set_incremental_info(Incremental_script_entry* entry)
  221. { this->incremental_script_entry_ = entry; }
  222. // Return the filename.
  223. const std::string&
  224. filename() const
  225. { return this->filename_; }
  226. // Return the input file index.
  227. unsigned int
  228. input_file_index() const
  229. { return this->input_file_index_; }
  230. // Return the pointer to the incremental information for this script.
  231. Incremental_script_entry*
  232. incremental_info() const
  233. { return this->incremental_script_entry_; }
  234. private:
  235. const std::string filename_;
  236. unsigned int input_file_index_;
  237. Incremental_script_entry* incremental_script_entry_;
  238. };
  239. // Class for recording input scripts.
  240. class Incremental_script_entry : public Incremental_input_entry
  241. {
  242. public:
  243. Incremental_script_entry(Stringpool::Key filename_key,
  244. unsigned int arg_serial, Script_info* /*script*/,
  245. Timespec mtime)
  246. : Incremental_input_entry(filename_key, arg_serial, mtime),
  247. objects_()
  248. { }
  249. // Add a member object to the archive.
  250. void
  251. add_object(Incremental_input_entry* obj_entry)
  252. {
  253. this->objects_.push_back(obj_entry);
  254. }
  255. // Return the number of objects included by this script.
  256. unsigned int
  257. get_object_count()
  258. { return this->objects_.size(); }
  259. // Return the Nth object.
  260. Incremental_input_entry*
  261. get_object(unsigned int n)
  262. {
  263. gold_assert(n < this->objects_.size());
  264. return this->objects_[n];
  265. }
  266. protected:
  267. virtual Incremental_input_type
  268. do_type() const
  269. { return INCREMENTAL_INPUT_SCRIPT; }
  270. // Return a pointer to the derived Incremental_script_entry object.
  271. virtual Incremental_script_entry*
  272. do_script_entry()
  273. { return this; }
  274. private:
  275. // Objects that have been included by this script.
  276. std::vector<Incremental_input_entry*> objects_;
  277. };
  278. // Class for recording input object files.
  279. class Incremental_object_entry : public Incremental_input_entry
  280. {
  281. public:
  282. Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
  283. unsigned int arg_serial, Timespec mtime)
  284. : Incremental_input_entry(filename_key, arg_serial, mtime), obj_(obj),
  285. is_member_(false), sections_(), groups_()
  286. { this->sections_.reserve(obj->shnum()); }
  287. // Get the object.
  288. Object*
  289. object() const
  290. { return this->obj_; }
  291. // Record that this object is an archive member.
  292. void
  293. set_is_member()
  294. { this->is_member_ = true; }
  295. // Return true if this object is an archive member.
  296. bool
  297. is_member() const
  298. { return this->is_member_; }
  299. // Add an input section.
  300. void
  301. add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
  302. { this->sections_.push_back(Input_section(shndx, name_key, sh_size)); }
  303. // Return the number of input sections in this object.
  304. unsigned int
  305. get_input_section_count() const
  306. { return this->sections_.size(); }
  307. // Return the input section index for the Nth input section.
  308. Stringpool::Key
  309. get_input_section_index(unsigned int n) const
  310. { return this->sections_[n].shndx_; }
  311. // Return the stringpool key of the Nth input section.
  312. Stringpool::Key
  313. get_input_section_name_key(unsigned int n) const
  314. { return this->sections_[n].name_key_; }
  315. // Return the size of the Nth input section.
  316. off_t
  317. get_input_section_size(unsigned int n) const
  318. { return this->sections_[n].sh_size_; }
  319. // Add a kept COMDAT group.
  320. void
  321. add_comdat_group(Stringpool::Key signature_key)
  322. { this->groups_.push_back(signature_key); }
  323. // Return the number of COMDAT groups.
  324. unsigned int
  325. get_comdat_group_count() const
  326. { return this->groups_.size(); }
  327. // Return the stringpool key for the signature of the Nth comdat group.
  328. Stringpool::Key
  329. get_comdat_signature_key(unsigned int n) const
  330. { return this->groups_[n]; }
  331. protected:
  332. virtual Incremental_input_type
  333. do_type() const
  334. {
  335. return (this->is_member_
  336. ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
  337. : INCREMENTAL_INPUT_OBJECT);
  338. }
  339. // Return a pointer to the derived Incremental_object_entry object.
  340. virtual Incremental_object_entry*
  341. do_object_entry()
  342. { return this; }
  343. private:
  344. // The object file itself.
  345. Object* obj_;
  346. // Whether this object is an archive member.
  347. bool is_member_;
  348. // Input sections.
  349. struct Input_section
  350. {
  351. Input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
  352. : shndx_(shndx), name_key_(name_key), sh_size_(sh_size)
  353. { }
  354. unsigned int shndx_;
  355. Stringpool::Key name_key_;
  356. off_t sh_size_;
  357. };
  358. std::vector<Input_section> sections_;
  359. // COMDAT groups.
  360. std::vector<Stringpool::Key> groups_;
  361. };
  362. // Class for recording shared library input files.
  363. class Incremental_dynobj_entry : public Incremental_input_entry
  364. {
  365. public:
  366. Incremental_dynobj_entry(Stringpool::Key filename_key,
  367. Stringpool::Key soname_key, Object* obj,
  368. unsigned int arg_serial, Timespec mtime)
  369. : Incremental_input_entry(filename_key, arg_serial, mtime),
  370. soname_key_(soname_key), obj_(obj)
  371. { }
  372. // Get the object.
  373. Object*
  374. object() const
  375. { return this->obj_; }
  376. // Get the stringpool key for the soname.
  377. Stringpool::Key
  378. get_soname_key() const
  379. { return this->soname_key_; }
  380. protected:
  381. virtual Incremental_input_type
  382. do_type() const
  383. { return INCREMENTAL_INPUT_SHARED_LIBRARY; }
  384. // Return a pointer to the derived Incremental_dynobj_entry object.
  385. virtual Incremental_dynobj_entry*
  386. do_dynobj_entry()
  387. { return this; }
  388. private:
  389. // Key of the soname string in the section stringtable.
  390. Stringpool::Key soname_key_;
  391. // The object file itself.
  392. Object* obj_;
  393. };
  394. // Class for recording archive library input files.
  395. class Incremental_archive_entry : public Incremental_input_entry
  396. {
  397. public:
  398. Incremental_archive_entry(Stringpool::Key filename_key,
  399. unsigned int arg_serial, Timespec mtime)
  400. : Incremental_input_entry(filename_key, arg_serial, mtime), members_(),
  401. unused_syms_()
  402. { }
  403. // Add a member object to the archive.
  404. void
  405. add_object(Incremental_object_entry* obj_entry)
  406. {
  407. this->members_.push_back(obj_entry);
  408. obj_entry->set_is_member();
  409. }
  410. // Add an unused global symbol to the archive.
  411. void
  412. add_unused_global_symbol(Stringpool::Key symbol_key)
  413. { this->unused_syms_.push_back(symbol_key); }
  414. // Return the number of member objects included in the link.
  415. unsigned int
  416. get_member_count()
  417. { return this->members_.size(); }
  418. // Return the Nth member object.
  419. Incremental_object_entry*
  420. get_member(unsigned int n)
  421. { return this->members_[n]; }
  422. // Return the number of unused global symbols in this archive.
  423. unsigned int
  424. get_unused_global_symbol_count()
  425. { return this->unused_syms_.size(); }
  426. // Return the Nth unused global symbol.
  427. Stringpool::Key
  428. get_unused_global_symbol(unsigned int n)
  429. { return this->unused_syms_[n]; }
  430. protected:
  431. virtual Incremental_input_type
  432. do_type() const
  433. { return INCREMENTAL_INPUT_ARCHIVE; }
  434. // Return a pointer to the derived Incremental_archive_entry object.
  435. virtual Incremental_archive_entry*
  436. do_archive_entry()
  437. { return this; }
  438. private:
  439. // Members of the archive that have been included in the link.
  440. std::vector<Incremental_object_entry*> members_;
  441. // Unused global symbols from this archive.
  442. std::vector<Stringpool::Key> unused_syms_;
  443. };
  444. // This class contains the information needed during an incremental
  445. // build about the inputs necessary to build the .gnu_incremental_inputs.
  446. class Incremental_inputs
  447. {
  448. public:
  449. typedef std::vector<Incremental_input_entry*> Input_list;
  450. Incremental_inputs()
  451. : inputs_(), command_line_(), command_line_key_(0),
  452. strtab_(new Stringpool()), current_object_(NULL),
  453. current_object_entry_(NULL), inputs_section_(NULL),
  454. symtab_section_(NULL), relocs_section_(NULL),
  455. reloc_count_(0)
  456. { }
  457. ~Incremental_inputs() { delete this->strtab_; }
  458. // Record the command line.
  459. void
  460. report_command_line(int argc, const char* const* argv);
  461. // Record the initial info for archive file ARCHIVE.
  462. void
  463. report_archive_begin(Library_base* arch, unsigned int arg_serial,
  464. Script_info* script_info);
  465. // Record the final info for archive file ARCHIVE.
  466. void
  467. report_archive_end(Library_base* arch);
  468. // Record the info for object file OBJ. If ARCH is not NULL,
  469. // attach the object file to the archive.
  470. void
  471. report_object(Object* obj, unsigned int arg_serial, Library_base* arch,
  472. Script_info* script_info);
  473. // Record an input section belonging to object file OBJ.
  474. void
  475. report_input_section(Object* obj, unsigned int shndx, const char* name,
  476. off_t sh_size);
  477. // Record a kept COMDAT group belonging to object file OBJ.
  478. void
  479. report_comdat_group(Object* obj, const char* name);
  480. // Record the info for input script SCRIPT.
  481. void
  482. report_script(Script_info* script, unsigned int arg_serial,
  483. Timespec mtime);
  484. // Return the running count of incremental relocations.
  485. unsigned int
  486. get_reloc_count() const
  487. { return this->reloc_count_; }
  488. // Update the running count of incremental relocations.
  489. void
  490. set_reloc_count(unsigned int count)
  491. { this->reloc_count_ = count; }
  492. // Prepare for layout. Called from Layout::finalize.
  493. void
  494. finalize();
  495. // Create the .gnu_incremental_inputs and related sections.
  496. void
  497. create_data_sections(Symbol_table* symtab);
  498. // Return the .gnu_incremental_inputs section.
  499. Output_section_data*
  500. inputs_section() const
  501. { return this->inputs_section_; }
  502. // Return the .gnu_incremental_symtab section.
  503. Output_data_space*
  504. symtab_section() const
  505. { return this->symtab_section_; }
  506. // Return the .gnu_incremental_relocs section.
  507. Output_data_space*
  508. relocs_section() const
  509. { return this->relocs_section_; }
  510. // Return the .gnu_incremental_got_plt section.
  511. Output_data_space*
  512. got_plt_section() const
  513. { return this->got_plt_section_; }
  514. // Return the .gnu_incremental_strtab stringpool.
  515. Stringpool*
  516. get_stringpool() const
  517. { return this->strtab_; }
  518. // Return the canonical form of the command line, as will be stored in
  519. // .gnu_incremental_strtab.
  520. const std::string&
  521. command_line() const
  522. { return this->command_line_; }
  523. // Return the stringpool key of the command line.
  524. Stringpool::Key
  525. command_line_key() const
  526. { return this->command_line_key_; }
  527. // Return the number of input files.
  528. int
  529. input_file_count() const
  530. { return this->inputs_.size(); }
  531. // Return the input files.
  532. const Input_list&
  533. input_files() const
  534. { return this->inputs_; }
  535. // Return the sh_entsize value for the .gnu_incremental_relocs section.
  536. unsigned int
  537. relocs_entsize() const;
  538. private:
  539. // The list of input files.
  540. Input_list inputs_;
  541. // Canonical form of the command line, as will be stored in
  542. // .gnu_incremental_strtab.
  543. std::string command_line_;
  544. // The key of the command line string in the string pool.
  545. Stringpool::Key command_line_key_;
  546. // The .gnu_incremental_strtab string pool associated with the
  547. // .gnu_incremental_inputs.
  548. Stringpool* strtab_;
  549. // Keep track of the object currently being processed.
  550. Object* current_object_;
  551. Incremental_object_entry* current_object_entry_;
  552. // The .gnu_incremental_inputs section.
  553. Output_section_data* inputs_section_;
  554. // The .gnu_incremental_symtab section.
  555. Output_data_space* symtab_section_;
  556. // The .gnu_incremental_relocs section.
  557. Output_data_space* relocs_section_;
  558. // The .gnu_incremental_got_plt section.
  559. Output_data_space* got_plt_section_;
  560. // Total count of incremental relocations. Updated during Scan_relocs
  561. // phase at the completion of each object file.
  562. unsigned int reloc_count_;
  563. };
  564. // Reader class for global symbol info from an object file entry in
  565. // the .gnu_incremental_inputs section.
  566. template<bool big_endian>
  567. class Incremental_global_symbol_reader
  568. {
  569. private:
  570. typedef elfcpp::Swap<32, big_endian> Swap32;
  571. public:
  572. Incremental_global_symbol_reader(const unsigned char* p)
  573. : p_(p)
  574. { }
  575. unsigned int
  576. output_symndx() const
  577. { return Swap32::readval(this->p_); }
  578. unsigned int
  579. shndx() const
  580. { return Swap32::readval(this->p_ + 4); }
  581. unsigned int
  582. next_offset() const
  583. { return Swap32::readval(this->p_ + 8); }
  584. unsigned int
  585. reloc_count() const
  586. { return Swap32::readval(this->p_ + 12); }
  587. unsigned int
  588. reloc_offset() const
  589. { return Swap32::readval(this->p_ + 16); }
  590. private:
  591. // Base address of the symbol entry.
  592. const unsigned char* p_;
  593. };
  594. // Reader class for .gnu_incremental_inputs section.
  595. template<int size, bool big_endian>
  596. class Incremental_inputs_reader
  597. {
  598. private:
  599. typedef elfcpp::Swap<size, big_endian> Swap;
  600. typedef elfcpp::Swap<16, big_endian> Swap16;
  601. typedef elfcpp::Swap<32, big_endian> Swap32;
  602. typedef elfcpp::Swap<64, big_endian> Swap64;
  603. public:
  604. // Size of the .gnu_incremental_inputs header.
  605. // (3 x 4-byte fields, plus 4 bytes padding.)
  606. static const unsigned int header_size = 16;
  607. // Size of an input file entry.
  608. // (2 x 4-byte fields, 1 x 12-byte field, 2 x 2-byte fields.)
  609. static const unsigned int input_entry_size = 24;
  610. // Size of the first part of the supplemental info block for
  611. // relocatable objects and archive members.
  612. // (7 x 4-byte fields, plus 4 bytes padding.)
  613. static const unsigned int object_info_size = 32;
  614. // Size of an input section entry.
  615. // (2 x 4-byte fields, 2 x address-sized fields.)
  616. static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
  617. // Size of a global symbol entry in the supplemental info block.
  618. // (5 x 4-byte fields.)
  619. static const unsigned int global_sym_entry_size = 20;
  620. Incremental_inputs_reader()
  621. : p_(NULL), strtab_(NULL, 0), input_file_count_(0)
  622. { }
  623. Incremental_inputs_reader(const unsigned char* p,
  624. const elfcpp::Elf_strtab& strtab)
  625. : p_(p), strtab_(strtab)
  626. { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
  627. // Return the version number.
  628. unsigned int
  629. version() const
  630. { return Swap32::readval(this->p_); }
  631. // Return the count of input file entries.
  632. unsigned int
  633. input_file_count() const
  634. { return this->input_file_count_; }
  635. // Return the command line.
  636. const char*
  637. command_line() const
  638. {
  639. unsigned int offset = Swap32::readval(this->p_ + 8);
  640. return this->get_string(offset);
  641. }
  642. // Reader class for an input file entry and its supplemental info.
  643. class Incremental_input_entry_reader
  644. {
  645. private:
  646. static const unsigned int object_info_size =
  647. Incremental_inputs_reader<size, big_endian>::object_info_size;
  648. static const unsigned int input_section_entry_size =
  649. Incremental_inputs_reader<size, big_endian>::input_section_entry_size;
  650. static const unsigned int global_sym_entry_size =
  651. Incremental_inputs_reader<size, big_endian>::global_sym_entry_size;
  652. public:
  653. Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
  654. unsigned int offset)
  655. : inputs_(inputs), offset_(offset)
  656. {
  657. this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
  658. this->flags_ = Swap16::readval(this->inputs_->p_ + offset + 20);
  659. }
  660. // Return the filename.
  661. const char*
  662. filename() const
  663. {
  664. unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
  665. return this->inputs_->get_string(offset);
  666. }
  667. // Return the argument serial number.
  668. unsigned int
  669. arg_serial() const
  670. {
  671. return Swap16::readval(this->inputs_->p_ + this->offset_ + 22);
  672. }
  673. // Return the timestamp.
  674. Timespec
  675. get_mtime() const
  676. {
  677. Timespec t;
  678. const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
  679. t.seconds = Swap64::readval(p);
  680. t.nanoseconds = Swap32::readval(p+8);
  681. return t;
  682. }
  683. // Return the type of input file.
  684. Incremental_input_type
  685. type() const
  686. { return static_cast<Incremental_input_type>(this->flags_ & 0xff); }
  687. // Return TRUE if the file was found in a system directory.
  688. bool
  689. is_in_system_directory() const
  690. { return (this->flags_ & INCREMENTAL_INPUT_IN_SYSTEM_DIR) != 0; }
  691. // Return TRUE if the file was linked with --as-needed.
  692. bool
  693. as_needed() const
  694. { return (this->flags_ & INCREMENTAL_INPUT_AS_NEEDED) != 0; }
  695. // Return the input section count -- for objects only.
  696. unsigned int
  697. get_input_section_count() const
  698. {
  699. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  700. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  701. return Swap32::readval(this->inputs_->p_ + this->info_offset_);
  702. }
  703. // Return the soname -- for shared libraries only.
  704. const char*
  705. get_soname() const
  706. {
  707. gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
  708. unsigned int offset = Swap32::readval(this->inputs_->p_
  709. + this->info_offset_);
  710. return this->inputs_->get_string(offset);
  711. }
  712. // Return the offset of the supplemental info for symbol SYMNDX --
  713. // for objects only.
  714. unsigned int
  715. get_symbol_offset(unsigned int symndx) const
  716. {
  717. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  718. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  719. unsigned int section_count = this->get_input_section_count();
  720. return (this->info_offset_
  721. + this->object_info_size
  722. + section_count * this->input_section_entry_size
  723. + symndx * this->global_sym_entry_size);
  724. }
  725. // Return the global symbol count -- for objects & shared libraries only.
  726. unsigned int
  727. get_global_symbol_count() const
  728. {
  729. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  730. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER
  731. || this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
  732. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
  733. }
  734. // Return the offset of the first local symbol -- for objects only.
  735. unsigned int
  736. get_local_symbol_offset() const
  737. {
  738. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  739. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  740. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 8);
  741. }
  742. // Return the local symbol count -- for objects only.
  743. unsigned int
  744. get_local_symbol_count() const
  745. {
  746. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  747. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  748. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 12);
  749. }
  750. // Return the index of the first dynamic relocation -- for objects only.
  751. unsigned int
  752. get_first_dyn_reloc() const
  753. {
  754. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  755. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  756. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 16);
  757. }
  758. // Return the dynamic relocation count -- for objects only.
  759. unsigned int
  760. get_dyn_reloc_count() const
  761. {
  762. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  763. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  764. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 20);
  765. }
  766. // Return the COMDAT group count -- for objects only.
  767. unsigned int
  768. get_comdat_group_count() const
  769. {
  770. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  771. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  772. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 24);
  773. }
  774. // Return the object count -- for scripts only.
  775. unsigned int
  776. get_object_count() const
  777. {
  778. gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
  779. return Swap32::readval(this->inputs_->p_ + this->info_offset_);
  780. }
  781. // Return the input file offset for object N -- for scripts only.
  782. unsigned int
  783. get_object_offset(unsigned int n) const
  784. {
  785. gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
  786. return Swap32::readval(this->inputs_->p_ + this->info_offset_
  787. + 4 + n * 4);
  788. }
  789. // Return the member count -- for archives only.
  790. unsigned int
  791. get_member_count() const
  792. {
  793. gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
  794. return Swap32::readval(this->inputs_->p_ + this->info_offset_);
  795. }
  796. // Return the unused symbol count -- for archives only.
  797. unsigned int
  798. get_unused_symbol_count() const
  799. {
  800. gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
  801. return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
  802. }
  803. // Return the input file offset for archive member N -- for archives only.
  804. unsigned int
  805. get_member_offset(unsigned int n) const
  806. {
  807. gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
  808. return Swap32::readval(this->inputs_->p_ + this->info_offset_
  809. + 8 + n * 4);
  810. }
  811. // Return the Nth unused global symbol -- for archives only.
  812. const char*
  813. get_unused_symbol(unsigned int n) const
  814. {
  815. gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
  816. unsigned int member_count = this->get_member_count();
  817. unsigned int offset = Swap32::readval(this->inputs_->p_
  818. + this->info_offset_ + 8
  819. + member_count * 4
  820. + n * 4);
  821. return this->inputs_->get_string(offset);
  822. }
  823. // Information about an input section.
  824. struct Input_section_info
  825. {
  826. const char* name;
  827. unsigned int output_shndx;
  828. off_t sh_offset;
  829. off_t sh_size;
  830. };
  831. // Return info about the Nth input section -- for objects only.
  832. Input_section_info
  833. get_input_section(unsigned int n) const
  834. {
  835. Input_section_info info;
  836. const unsigned char* p = (this->inputs_->p_
  837. + this->info_offset_
  838. + this->object_info_size
  839. + n * this->input_section_entry_size);
  840. unsigned int name_offset = Swap32::readval(p);
  841. info.name = this->inputs_->get_string(name_offset);
  842. info.output_shndx = Swap32::readval(p + 4);
  843. info.sh_offset = Swap::readval(p + 8);
  844. info.sh_size = Swap::readval(p + 8 + size / 8);
  845. return info;
  846. }
  847. // Return info about the Nth global symbol -- for objects only.
  848. Incremental_global_symbol_reader<big_endian>
  849. get_global_symbol_reader(unsigned int n) const
  850. {
  851. gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
  852. || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
  853. unsigned int section_count = this->get_input_section_count();
  854. const unsigned char* p = (this->inputs_->p_
  855. + this->info_offset_
  856. + this->object_info_size
  857. + section_count * this->input_section_entry_size
  858. + n * this->global_sym_entry_size);
  859. return Incremental_global_symbol_reader<big_endian>(p);
  860. }
  861. // Return the signature of the Nth comdat group -- for objects only.
  862. const char*
  863. get_comdat_group_signature(unsigned int n) const
  864. {
  865. unsigned int section_count = this->get_input_section_count();
  866. unsigned int symbol_count = this->get_global_symbol_count();
  867. const unsigned char* p = (this->inputs_->p_
  868. + this->info_offset_
  869. + this->object_info_size
  870. + section_count * this->input_section_entry_size
  871. + symbol_count * this->global_sym_entry_size
  872. + n * 4);
  873. unsigned int name_offset = Swap32::readval(p);
  874. return this->inputs_->get_string(name_offset);
  875. }
  876. // Return the output symbol index for the Nth global symbol -- for shared
  877. // libraries only. Sets *IS_DEF to TRUE if the symbol is defined in this
  878. // input file. Sets *IS_COPY to TRUE if the symbol was copied from this
  879. // input file with a COPY relocation.
  880. unsigned int
  881. get_output_symbol_index(unsigned int n, bool* is_def, bool* is_copy)
  882. {
  883. gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
  884. const unsigned char* p = (this->inputs_->p_
  885. + this->info_offset_ + 8
  886. + n * 4);
  887. unsigned int output_symndx = Swap32::readval(p);
  888. unsigned int flags = output_symndx >> INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
  889. output_symndx &= ((1U << INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT) - 1);
  890. switch (flags)
  891. {
  892. case INCREMENTAL_SHLIB_SYM_DEF:
  893. *is_def = true;
  894. *is_copy = false;
  895. break;
  896. case INCREMENTAL_SHLIB_SYM_COPY:
  897. *is_def = true;
  898. *is_copy = true;
  899. break;
  900. default:
  901. *is_def = false;
  902. *is_copy = false;
  903. }
  904. return output_symndx;
  905. }
  906. private:
  907. // The reader instance for the containing section.
  908. const Incremental_inputs_reader* inputs_;
  909. // The flags, including the type of input file.
  910. unsigned int flags_;
  911. // Section offset to the input file entry.
  912. unsigned int offset_;
  913. // Section offset to the supplemental info for the input file.
  914. unsigned int info_offset_;
  915. };
  916. // Return the offset of an input file entry given its index N.
  917. unsigned int
  918. input_file_offset(unsigned int n) const
  919. {
  920. gold_assert(n < this->input_file_count_);
  921. return this->header_size + n * this->input_entry_size;
  922. }
  923. // Return the index of an input file entry given its OFFSET.
  924. unsigned int
  925. input_file_index(unsigned int offset) const
  926. {
  927. int n = ((offset - this->header_size) / this->input_entry_size);
  928. gold_assert(input_file_offset(n) == offset);
  929. return n;
  930. }
  931. // Return a reader for the Nth input file entry.
  932. Incremental_input_entry_reader
  933. input_file(unsigned int n) const
  934. { return Incremental_input_entry_reader(this, this->input_file_offset(n)); }
  935. // Return a reader for the input file entry at OFFSET.
  936. Incremental_input_entry_reader
  937. input_file_at_offset(unsigned int offset) const
  938. {
  939. gold_assert(offset < (this->header_size
  940. + this->input_file_count_ * this->input_entry_size));
  941. return Incremental_input_entry_reader(this, offset);
  942. }
  943. // Return a reader for the global symbol info at OFFSET.
  944. Incremental_global_symbol_reader<big_endian>
  945. global_symbol_reader_at_offset(unsigned int offset) const
  946. {
  947. const unsigned char* p = this->p_ + offset;
  948. return Incremental_global_symbol_reader<big_endian>(p);
  949. }
  950. private:
  951. // Lookup a string in the ELF string table.
  952. const char* get_string(unsigned int offset) const
  953. {
  954. const char* s;
  955. if (this->strtab_.get_c_string(offset, &s))
  956. return s;
  957. return NULL;
  958. }
  959. // Base address of the .gnu_incremental_inputs section.
  960. const unsigned char* p_;
  961. // The associated ELF string table.
  962. elfcpp::Elf_strtab strtab_;
  963. // The number of input file entries in this section.
  964. unsigned int input_file_count_;
  965. };
  966. // Reader class for the .gnu_incremental_symtab section.
  967. template<bool big_endian>
  968. class Incremental_symtab_reader
  969. {
  970. public:
  971. Incremental_symtab_reader()
  972. : p_(NULL), len_(0)
  973. { }
  974. Incremental_symtab_reader(const unsigned char* p, off_t len)
  975. : p_(p), len_(len)
  976. { }
  977. // Return the count of symbols in this section.
  978. unsigned int
  979. symbol_count() const
  980. { return static_cast<unsigned int>(this->len_ / 4); }
  981. // Return the list head for symbol table entry N.
  982. unsigned int
  983. get_list_head(unsigned int n) const
  984. { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
  985. private:
  986. // Base address of the .gnu_incremental_relocs section.
  987. const unsigned char* p_;
  988. // Size of the section.
  989. off_t len_;
  990. };
  991. // Reader class for the .gnu_incremental_relocs section.
  992. template<int size, bool big_endian>
  993. class Incremental_relocs_reader
  994. {
  995. private:
  996. // Size of each field.
  997. static const unsigned int field_size = size / 8;
  998. public:
  999. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1000. typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
  1001. // Size of each entry.
  1002. static const unsigned int reloc_size = 8 + 2 * field_size;
  1003. Incremental_relocs_reader()
  1004. : p_(NULL), len_(0)
  1005. { }
  1006. Incremental_relocs_reader(const unsigned char* p, off_t len)
  1007. : p_(p), len_(len)
  1008. { }
  1009. // Return the count of relocations in this section.
  1010. unsigned int
  1011. reloc_count() const
  1012. { return static_cast<unsigned int>(this->len_ / reloc_size); }
  1013. // Return the relocation type for relocation entry at offset OFF.
  1014. unsigned int
  1015. get_r_type(unsigned int off) const
  1016. { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off); }
  1017. // Return the output section index for relocation entry at offset OFF.
  1018. unsigned int
  1019. get_r_shndx(unsigned int off) const
  1020. { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4); }
  1021. // Return the output section offset for relocation entry at offset OFF.
  1022. Address
  1023. get_r_offset(unsigned int off) const
  1024. { return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8); }
  1025. // Return the addend for relocation entry at offset OFF.
  1026. Addend
  1027. get_r_addend(unsigned int off) const
  1028. {
  1029. return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
  1030. + this->field_size);
  1031. }
  1032. // Return a pointer to the relocation entry at offset OFF.
  1033. const unsigned char*
  1034. data(unsigned int off) const
  1035. { return this->p_ + off; }
  1036. private:
  1037. // Base address of the .gnu_incremental_relocs section.
  1038. const unsigned char* p_;
  1039. // Size of the section.
  1040. off_t len_;
  1041. };
  1042. // Reader class for the .gnu_incremental_got_plt section.
  1043. template<bool big_endian>
  1044. class Incremental_got_plt_reader
  1045. {
  1046. public:
  1047. Incremental_got_plt_reader()
  1048. : p_(NULL), got_count_(0), got_desc_p_(NULL), plt_desc_p_(NULL)
  1049. { }
  1050. Incremental_got_plt_reader(const unsigned char* p) : p_(p)
  1051. {
  1052. this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
  1053. this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
  1054. this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 8;
  1055. }
  1056. // Return the GOT entry count.
  1057. unsigned int
  1058. get_got_entry_count() const
  1059. {
  1060. return this->got_count_;
  1061. }
  1062. // Return the PLT entry count.
  1063. unsigned int
  1064. get_plt_entry_count() const
  1065. {
  1066. return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
  1067. }
  1068. // Return the GOT type for GOT entry N.
  1069. unsigned int
  1070. get_got_type(unsigned int n)
  1071. {
  1072. return this->p_[8 + n];
  1073. }
  1074. // Return the symbol index for GOT entry N.
  1075. unsigned int
  1076. get_got_symndx(unsigned int n)
  1077. {
  1078. return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8);
  1079. }
  1080. // Return the input file index for GOT entry N.
  1081. unsigned int
  1082. get_got_input_index(unsigned int n)
  1083. {
  1084. return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8 + 4);
  1085. }
  1086. // Return the PLT descriptor for PLT entry N.
  1087. unsigned int
  1088. get_plt_desc(unsigned int n)
  1089. {
  1090. return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
  1091. }
  1092. private:
  1093. // Base address of the .gnu_incremental_got_plt section.
  1094. const unsigned char* p_;
  1095. // GOT entry count.
  1096. unsigned int got_count_;
  1097. // Base address of the GOT descriptor array.
  1098. const unsigned char* got_desc_p_;
  1099. // Base address of the PLT descriptor array.
  1100. const unsigned char* plt_desc_p_;
  1101. };
  1102. // An object representing the ELF file we edit during an incremental build.
  1103. // Similar to Object or Dynobj, but operates on Output_file and contains
  1104. // methods to support incremental updating. This is the abstract parent class
  1105. // implemented in Sized_incremental_binary<size, big_endian> for a specific
  1106. // endianness and size.
  1107. class Incremental_binary
  1108. {
  1109. public:
  1110. Incremental_binary(Output_file* output, Target* /*target*/)
  1111. : input_args_map_(), library_map_(), script_map_(),
  1112. output_(output)
  1113. { }
  1114. virtual
  1115. ~Incremental_binary()
  1116. { }
  1117. // Check the .gnu_incremental_inputs section to see whether an incremental
  1118. // build is possible.
  1119. bool
  1120. check_inputs(const Command_line& cmdline,
  1121. Incremental_inputs* incremental_inputs)
  1122. { return this->do_check_inputs(cmdline, incremental_inputs); }
  1123. // Report an error.
  1124. void
  1125. error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
  1126. // Proxy class for a sized Incremental_input_entry_reader.
  1127. class Input_reader
  1128. {
  1129. public:
  1130. Input_reader()
  1131. { }
  1132. virtual
  1133. ~Input_reader()
  1134. { }
  1135. const char*
  1136. filename() const
  1137. { return this->do_filename(); }
  1138. Timespec
  1139. get_mtime() const
  1140. { return this->do_get_mtime(); }
  1141. Incremental_input_type
  1142. type() const
  1143. { return this->do_type(); }
  1144. unsigned int
  1145. arg_serial() const
  1146. { return this->do_arg_serial(); }
  1147. unsigned int
  1148. get_unused_symbol_count() const
  1149. { return this->do_get_unused_symbol_count(); }
  1150. const char*
  1151. get_unused_symbol(unsigned int n) const
  1152. { return this->do_get_unused_symbol(n); }
  1153. protected:
  1154. virtual const char*
  1155. do_filename() const = 0;
  1156. virtual Timespec
  1157. do_get_mtime() const = 0;
  1158. virtual Incremental_input_type
  1159. do_type() const = 0;
  1160. virtual unsigned int
  1161. do_arg_serial() const = 0;
  1162. virtual unsigned int
  1163. do_get_unused_symbol_count() const = 0;
  1164. virtual const char*
  1165. do_get_unused_symbol(unsigned int n) const = 0;
  1166. };
  1167. // Return the number of input files.
  1168. unsigned int
  1169. input_file_count() const
  1170. { return this->do_input_file_count(); }
  1171. // Return an Input_reader for input file N.
  1172. const Input_reader*
  1173. get_input_reader(unsigned int n) const
  1174. { return this->do_get_input_reader(n); }
  1175. // Return TRUE if the input file N has changed since the last link.
  1176. bool
  1177. file_has_changed(unsigned int n) const
  1178. { return this->do_file_has_changed(n); }
  1179. // Return the Input_argument for input file N. Returns NULL if
  1180. // the Input_argument is not available.
  1181. const Input_argument*
  1182. get_input_argument(unsigned int n) const
  1183. {
  1184. const Input_reader* input_file = this->do_get_input_reader(n);
  1185. unsigned int arg_serial = input_file->arg_serial();
  1186. if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
  1187. return NULL;
  1188. return this->input_args_map_[arg_serial - 1];
  1189. }
  1190. // Return an Incremental_library for the given input file.
  1191. Incremental_library*
  1192. get_library(unsigned int n) const
  1193. { return this->library_map_[n]; }
  1194. // Return a Script_info for the given input file.
  1195. Script_info*
  1196. get_script_info(unsigned int n) const
  1197. { return this->script_map_[n]; }
  1198. // Initialize the layout of the output file based on the existing
  1199. // output file.
  1200. void
  1201. init_layout(Layout* layout)
  1202. { this->do_init_layout(layout); }
  1203. // Mark regions of the input file that must be kept unchanged.
  1204. void
  1205. reserve_layout(unsigned int input_file_index)
  1206. { this->do_reserve_layout(input_file_index); }
  1207. // Process the GOT and PLT entries from the existing output file.
  1208. void
  1209. process_got_plt(Symbol_table* symtab, Layout* layout)
  1210. { this->do_process_got_plt(symtab, layout); }
  1211. // Emit COPY relocations from the existing output file.
  1212. void
  1213. emit_copy_relocs(Symbol_table* symtab)
  1214. { this->do_emit_copy_relocs(symtab); }
  1215. // Apply incremental relocations for symbols whose values have changed.
  1216. void
  1217. apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
  1218. Output_file* of)
  1219. { this->do_apply_incremental_relocs(symtab, layout, of); }
  1220. // Functions and types for the elfcpp::Elf_file interface. This
  1221. // permit us to use Incremental_binary as the File template parameter for
  1222. // elfcpp::Elf_file.
  1223. // The View class is returned by view. It must support a single
  1224. // method, data(). This is trivial, because Output_file::get_output_view
  1225. // does what we need.
  1226. class View
  1227. {
  1228. public:
  1229. View(const unsigned char* p)
  1230. : p_(p)
  1231. { }
  1232. const unsigned char*
  1233. data() const
  1234. { return this->p_; }
  1235. private:
  1236. const unsigned char* p_;
  1237. };
  1238. // Return a View.
  1239. View
  1240. view(off_t file_offset, section_size_type data_size)
  1241. { return View(this->output_->get_input_view(file_offset, data_size)); }
  1242. // A location in the file.
  1243. struct Location
  1244. {
  1245. off_t file_offset;
  1246. off_t data_size;
  1247. Location(off_t fo, section_size_type ds)
  1248. : file_offset(fo), data_size(ds)
  1249. { }
  1250. Location()
  1251. : file_offset(0), data_size(0)
  1252. { }
  1253. };
  1254. // Get a View given a Location.
  1255. View
  1256. view(Location loc)
  1257. { return View(this->view(loc.file_offset, loc.data_size)); }
  1258. // Return the Output_file.
  1259. Output_file*
  1260. output_file()
  1261. { return this->output_; }
  1262. protected:
  1263. // Check the .gnu_incremental_inputs section to see whether an incremental
  1264. // build is possible.
  1265. virtual bool
  1266. do_check_inputs(const Command_line& cmdline,
  1267. Incremental_inputs* incremental_inputs) = 0;
  1268. // Return TRUE if input file N has changed since the last incremental link.
  1269. virtual bool
  1270. do_file_has_changed(unsigned int n) const = 0;
  1271. // Initialize the layout of the output file based on the existing
  1272. // output file.
  1273. virtual void
  1274. do_init_layout(Layout* layout) = 0;
  1275. // Mark regions of the input file that must be kept unchanged.
  1276. virtual void
  1277. do_reserve_layout(unsigned int input_file_index) = 0;
  1278. // Process the GOT and PLT entries from the existing output file.
  1279. virtual void
  1280. do_process_got_plt(Symbol_table* symtab, Layout* layout) = 0;
  1281. // Emit COPY relocations from the existing output file.
  1282. virtual void
  1283. do_emit_copy_relocs(Symbol_table* symtab) = 0;
  1284. // Apply incremental relocations for symbols whose values have changed.
  1285. virtual void
  1286. do_apply_incremental_relocs(const Symbol_table*, Layout*, Output_file*) = 0;
  1287. virtual unsigned int
  1288. do_input_file_count() const = 0;
  1289. virtual const Input_reader*
  1290. do_get_input_reader(unsigned int) const = 0;
  1291. // Map from input file index to Input_argument.
  1292. std::vector<const Input_argument*> input_args_map_;
  1293. // Map from an input file index to an Incremental_library.
  1294. std::vector<Incremental_library*> library_map_;
  1295. // Map from an input file index to a Script_info.
  1296. std::vector<Script_info*> script_map_;
  1297. private:
  1298. // Edited output file object.
  1299. Output_file* output_;
  1300. };
  1301. template<int size, bool big_endian>
  1302. class Sized_relobj_incr;
  1303. template<int size, bool big_endian>
  1304. class Sized_incremental_binary : public Incremental_binary
  1305. {
  1306. public:
  1307. Sized_incremental_binary(Output_file* output,
  1308. const elfcpp::Ehdr<size, big_endian>& ehdr,
  1309. Target* target)
  1310. : Incremental_binary(output, target), elf_file_(this, ehdr),
  1311. input_objects_(), section_map_(), symbol_map_(), copy_relocs_(),
  1312. main_symtab_loc_(), main_strtab_loc_(), has_incremental_info_(false),
  1313. inputs_reader_(), symtab_reader_(), relocs_reader_(), got_plt_reader_(),
  1314. input_entry_readers_()
  1315. { this->setup_readers(); }
  1316. // Returns TRUE if the file contains incremental info.
  1317. bool
  1318. has_incremental_info() const
  1319. { return this->has_incremental_info_; }
  1320. // Record a pointer to the object for input file N.
  1321. void
  1322. set_input_object(unsigned int n,
  1323. Sized_relobj_incr<size, big_endian>* obj)
  1324. { this->input_objects_[n] = obj; }
  1325. // Return a pointer to the object for input file N.
  1326. Sized_relobj_incr<size, big_endian>*
  1327. input_object(unsigned int n) const
  1328. {
  1329. gold_assert(n < this->input_objects_.size());
  1330. return this->input_objects_[n];
  1331. }
  1332. // Return the Output_section for section index SHNDX.
  1333. Output_section*
  1334. output_section(unsigned int shndx)
  1335. { return this->section_map_[shndx]; }
  1336. // Map a symbol table entry from the base file to the output symbol table.
  1337. // SYMNDX is relative to the first forced-local or global symbol in the
  1338. // input file symbol table.
  1339. void
  1340. add_global_symbol(unsigned int symndx, Symbol* gsym)
  1341. { this->symbol_map_[symndx] = gsym; }
  1342. // Map a symbol table entry from the base file to the output symbol table.
  1343. // SYMNDX is relative to the first forced-local or global symbol in the
  1344. // input file symbol table.
  1345. Symbol*
  1346. global_symbol(unsigned int symndx) const
  1347. { return this->symbol_map_[symndx]; }
  1348. // Add a COPY relocation for a global symbol.
  1349. void
  1350. add_copy_reloc(Symbol* gsym, Output_section* os, off_t offset)
  1351. { this->copy_relocs_.push_back(Copy_reloc(gsym, os, offset)); }
  1352. // Readers for the incremental info sections.
  1353. const Incremental_inputs_reader<size, big_endian>&
  1354. inputs_reader() const
  1355. { return this->inputs_reader_; }
  1356. const Incremental_symtab_reader<big_endian>&
  1357. symtab_reader() const
  1358. { return this->symtab_reader_; }
  1359. const Incremental_relocs_reader<size, big_endian>&
  1360. relocs_reader() const
  1361. { return this->relocs_reader_; }
  1362. const Incremental_got_plt_reader<big_endian>&
  1363. got_plt_reader() const
  1364. { return this->got_plt_reader_; }
  1365. void
  1366. get_symtab_view(View* symtab_view, unsigned int* sym_count,
  1367. elfcpp::Elf_strtab* strtab);
  1368. protected:
  1369. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  1370. typedef typename Inputs_reader::Incremental_input_entry_reader
  1371. Input_entry_reader;
  1372. virtual bool
  1373. do_check_inputs(const Command_line& cmdline,
  1374. Incremental_inputs* incremental_inputs);
  1375. // Return TRUE if input file N has changed since the last incremental link.
  1376. virtual bool
  1377. do_file_has_changed(unsigned int n) const;
  1378. // Initialize the layout of the output file based on the existing
  1379. // output file.
  1380. virtual void
  1381. do_init_layout(Layout* layout);
  1382. // Mark regions of the input file that must be kept unchanged.
  1383. virtual void
  1384. do_reserve_layout(unsigned int input_file_index);
  1385. // Process the GOT and PLT entries from the existing output file.
  1386. virtual void
  1387. do_process_got_plt(Symbol_table* symtab, Layout* layout);
  1388. // Emit COPY relocations from the existing output file.
  1389. virtual void
  1390. do_emit_copy_relocs(Symbol_table* symtab);
  1391. // Apply incremental relocations for symbols whose values have changed.
  1392. virtual void
  1393. do_apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
  1394. Output_file* of);
  1395. // Proxy class for a sized Incremental_input_entry_reader.
  1396. class Sized_input_reader : public Input_reader
  1397. {
  1398. public:
  1399. Sized_input_reader(Input_entry_reader r)
  1400. : Input_reader(), reader_(r)
  1401. { }
  1402. virtual
  1403. ~Sized_input_reader()
  1404. { }
  1405. private:
  1406. const char*
  1407. do_filename() const
  1408. { return this->reader_.filename(); }
  1409. Timespec
  1410. do_get_mtime() const
  1411. { return this->reader_.get_mtime(); }
  1412. Incremental_input_type
  1413. do_type() const
  1414. { return this->reader_.type(); }
  1415. unsigned int
  1416. do_arg_serial() const
  1417. { return this->reader_.arg_serial(); }
  1418. unsigned int
  1419. do_get_unused_symbol_count() const
  1420. { return this->reader_.get_unused_symbol_count(); }
  1421. const char*
  1422. do_get_unused_symbol(unsigned int n) const
  1423. { return this->reader_.get_unused_symbol(n); }
  1424. Input_entry_reader reader_;
  1425. };
  1426. virtual unsigned int
  1427. do_input_file_count() const
  1428. { return this->inputs_reader_.input_file_count(); }
  1429. virtual const Input_reader*
  1430. do_get_input_reader(unsigned int n) const
  1431. {
  1432. gold_assert(n < this->input_entry_readers_.size());
  1433. return &this->input_entry_readers_[n];
  1434. }
  1435. private:
  1436. // List of symbols that need COPY relocations.
  1437. struct Copy_reloc
  1438. {
  1439. Copy_reloc(Symbol* sym, Output_section* os, off_t off)
  1440. : symbol(sym), output_section(os), offset(off)
  1441. { }
  1442. // The global symbol to copy.
  1443. Symbol* symbol;
  1444. // The output section into which the symbol was copied.
  1445. Output_section* output_section;
  1446. // The offset within that output section.
  1447. off_t offset;
  1448. };
  1449. typedef std::vector<Copy_reloc> Copy_relocs;
  1450. bool
  1451. find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
  1452. unsigned int* p_symtab_shndx,
  1453. unsigned int* p_relocs_shndx,
  1454. unsigned int* p_got_plt_shndx,
  1455. unsigned int* p_strtab_shndx);
  1456. void
  1457. setup_readers();
  1458. // Output as an ELF file.
  1459. elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
  1460. // Vector of pointers to the input objects for the unchanged files.
  1461. // For replaced files, the corresponding pointer is NULL.
  1462. std::vector<Sized_relobj_incr<size, big_endian>*> input_objects_;
  1463. // Map section index to an Output_section in the updated layout.
  1464. std::vector<Output_section*> section_map_;
  1465. // Map global symbols from the input file to the symbol table.
  1466. std::vector<Symbol*> symbol_map_;
  1467. // List of symbols that need COPY relocations.
  1468. Copy_relocs copy_relocs_;
  1469. // Locations of the main symbol table and symbol string table.
  1470. Location main_symtab_loc_;
  1471. Location main_strtab_loc_;
  1472. // Readers for the incremental info sections.
  1473. bool has_incremental_info_;
  1474. Incremental_inputs_reader<size, big_endian> inputs_reader_;
  1475. Incremental_symtab_reader<big_endian> symtab_reader_;
  1476. Incremental_relocs_reader<size, big_endian> relocs_reader_;
  1477. Incremental_got_plt_reader<big_endian> got_plt_reader_;
  1478. std::vector<Sized_input_reader> input_entry_readers_;
  1479. };
  1480. // An incremental Relobj. This class represents a relocatable object
  1481. // that has not changed since the last incremental link, and whose contents
  1482. // can be used directly from the base file.
  1483. template<int size, bool big_endian>
  1484. class Sized_relobj_incr : public Sized_relobj<size, big_endian>
  1485. {
  1486. public:
  1487. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1488. typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
  1489. Sized_relobj_incr(const std::string& name,
  1490. Sized_incremental_binary<size, big_endian>* ibase,
  1491. unsigned int input_file_index);
  1492. private:
  1493. // For convenience.
  1494. typedef Sized_relobj_incr<size, big_endian> This;
  1495. static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1496. typedef typename Sized_relobj<size, big_endian>::Output_sections
  1497. Output_sections;
  1498. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  1499. typedef typename Inputs_reader::Incremental_input_entry_reader
  1500. Input_entry_reader;
  1501. // A local symbol.
  1502. struct Local_symbol
  1503. {
  1504. Local_symbol(const char* name_, Address value_, unsigned int size_,
  1505. unsigned int shndx_, unsigned int type_,
  1506. bool needs_dynsym_entry_)
  1507. : st_value(value_), name(name_), st_size(size_), st_shndx(shndx_),
  1508. st_type(type_), output_dynsym_index(0),
  1509. needs_dynsym_entry(needs_dynsym_entry_)
  1510. { }
  1511. // The symbol value.
  1512. Address st_value;
  1513. // The symbol name. This points to the stringpool entry.
  1514. const char* name;
  1515. // The symbol size.
  1516. unsigned int st_size;
  1517. // The output section index.
  1518. unsigned int st_shndx : 28;
  1519. // The symbol type.
  1520. unsigned int st_type : 4;
  1521. // The index of the symbol in the output dynamic symbol table.
  1522. unsigned int output_dynsym_index : 31;
  1523. // TRUE if the symbol needs to appear in the dynamic symbol table.
  1524. unsigned int needs_dynsym_entry : 1;
  1525. };
  1526. // Return TRUE if this is an incremental (unchanged) input file.
  1527. bool
  1528. do_is_incremental() const
  1529. { return true; }
  1530. // Return the last modified time of the file.
  1531. Timespec
  1532. do_get_mtime()
  1533. { return this->input_reader_.get_mtime(); }
  1534. // Read the symbols.
  1535. void
  1536. do_read_symbols(Read_symbols_data*);
  1537. // Lay out the input sections.
  1538. void
  1539. do_layout(Symbol_table*, Layout*, Read_symbols_data*);
  1540. // Layout sections whose layout was deferred while waiting for
  1541. // input files from a plugin.
  1542. void
  1543. do_layout_deferred_sections(Layout*);
  1544. // Add the symbols to the symbol table.
  1545. void
  1546. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
  1547. Archive::Should_include
  1548. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  1549. std::string* why);
  1550. // Iterate over global symbols, calling a visitor class V for each.
  1551. void
  1552. do_for_all_global_symbols(Read_symbols_data* sd,
  1553. Library_base::Symbol_visitor_base* v);
  1554. // Get the size of a section.
  1555. uint64_t
  1556. do_section_size(unsigned int shndx);
  1557. // Get the name of a section.
  1558. std::string
  1559. do_section_name(unsigned int shndx) const;
  1560. // Return a view of the contents of a section.
  1561. const unsigned char*
  1562. do_section_contents(unsigned int shndx, section_size_type* plen,
  1563. bool cache);
  1564. // Return section flags.
  1565. uint64_t
  1566. do_section_flags(unsigned int shndx);
  1567. // Return section entsize.
  1568. uint64_t
  1569. do_section_entsize(unsigned int shndx);
  1570. // Return section address.
  1571. uint64_t
  1572. do_section_address(unsigned int shndx);
  1573. // Return section type.
  1574. unsigned int
  1575. do_section_type(unsigned int shndx);
  1576. // Return the section link field.
  1577. unsigned int
  1578. do_section_link(unsigned int shndx);
  1579. // Return the section link field.
  1580. unsigned int
  1581. do_section_info(unsigned int shndx);
  1582. // Return the section alignment.
  1583. uint64_t
  1584. do_section_addralign(unsigned int shndx);
  1585. // Return the Xindex structure to use.
  1586. Xindex*
  1587. do_initialize_xindex();
  1588. // Get symbol counts.
  1589. void
  1590. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
  1591. // Get global symbols.
  1592. const Symbols*
  1593. do_get_global_symbols() const
  1594. { return &this->symbols_; }
  1595. // Return the value of a local symbol.
  1596. uint64_t
  1597. do_local_symbol_value(unsigned int, uint64_t) const
  1598. { gold_unreachable(); }
  1599. unsigned int
  1600. do_local_plt_offset(unsigned int) const
  1601. { gold_unreachable(); }
  1602. bool
  1603. do_local_is_tls(unsigned int) const
  1604. { gold_unreachable(); }
  1605. // Return the number of local symbols.
  1606. unsigned int
  1607. do_local_symbol_count() const
  1608. { return this->local_symbol_count_; }
  1609. // Return the number of local symbols in the output symbol table.
  1610. unsigned int
  1611. do_output_local_symbol_count() const
  1612. { return this->local_symbol_count_; }
  1613. // Return the file offset for local symbols in the output symbol table.
  1614. off_t
  1615. do_local_symbol_offset() const
  1616. { return this->local_symbol_offset_; }
  1617. // Read the relocs.
  1618. void
  1619. do_read_relocs(Read_relocs_data*);
  1620. // Process the relocs to find list of referenced sections. Used only
  1621. // during garbage collection.
  1622. void
  1623. do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  1624. // Scan the relocs and adjust the symbol table.
  1625. void
  1626. do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  1627. // Count the local symbols.
  1628. void
  1629. do_count_local_symbols(Stringpool_template<char>*,
  1630. Stringpool_template<char>*);
  1631. // Finalize the local symbols.
  1632. unsigned int
  1633. do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
  1634. // Set the offset where local dynamic symbol information will be stored.
  1635. unsigned int
  1636. do_set_local_dynsym_indexes(unsigned int);
  1637. // Set the offset where local dynamic symbol information will be stored.
  1638. unsigned int
  1639. do_set_local_dynsym_offset(off_t);
  1640. // Relocate the input sections and write out the local symbols.
  1641. void
  1642. do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
  1643. // Set the offset of a section.
  1644. void
  1645. do_set_section_offset(unsigned int shndx, uint64_t off);
  1646. // The Incremental_binary base file.
  1647. Sized_incremental_binary<size, big_endian>* ibase_;
  1648. // The index of the object in the input file list.
  1649. unsigned int input_file_index_;
  1650. // The reader for the input file.
  1651. Input_entry_reader input_reader_;
  1652. // The number of local symbols.
  1653. unsigned int local_symbol_count_;
  1654. // The number of local symbols which go into the output file's dynamic
  1655. // symbol table.
  1656. unsigned int output_local_dynsym_count_;
  1657. // This starting symbol index in the output symbol table.
  1658. unsigned int local_symbol_index_;
  1659. // The file offset for local symbols in the output symbol table.
  1660. unsigned int local_symbol_offset_;
  1661. // The file offset for local symbols in the output symbol table.
  1662. unsigned int local_dynsym_offset_;
  1663. // The entries in the symbol table for the external symbols.
  1664. Symbols symbols_;
  1665. // Number of symbols defined in object file itself.
  1666. size_t defined_count_;
  1667. // The offset of the first incremental relocation for this object.
  1668. unsigned int incr_reloc_offset_;
  1669. // The number of incremental relocations for this object.
  1670. unsigned int incr_reloc_count_;
  1671. // The index of the first incremental relocation for this object in the
  1672. // updated output file.
  1673. unsigned int incr_reloc_output_index_;
  1674. // A copy of the incremental relocations from this object.
  1675. unsigned char* incr_relocs_;
  1676. // The local symbols.
  1677. std::vector<Local_symbol> local_symbols_;
  1678. };
  1679. // An incremental Dynobj. This class represents a shared object that has
  1680. // not changed since the last incremental link, and whose contents can be
  1681. // used directly from the base file.
  1682. template<int size, bool big_endian>
  1683. class Sized_incr_dynobj : public Dynobj
  1684. {
  1685. public:
  1686. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1687. static const Address invalid_address = static_cast<Address>(0) - 1;
  1688. Sized_incr_dynobj(const std::string& name,
  1689. Sized_incremental_binary<size, big_endian>* ibase,
  1690. unsigned int input_file_index);
  1691. private:
  1692. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  1693. typedef typename Inputs_reader::Incremental_input_entry_reader
  1694. Input_entry_reader;
  1695. // Return TRUE if this is an incremental (unchanged) input file.
  1696. bool
  1697. do_is_incremental() const
  1698. { return true; }
  1699. // Return the last modified time of the file.
  1700. Timespec
  1701. do_get_mtime()
  1702. { return this->input_reader_.get_mtime(); }
  1703. // Read the symbols.
  1704. void
  1705. do_read_symbols(Read_symbols_data*);
  1706. // Lay out the input sections.
  1707. void
  1708. do_layout(Symbol_table*, Layout*, Read_symbols_data*);
  1709. // Add the symbols to the symbol table.
  1710. void
  1711. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
  1712. Archive::Should_include
  1713. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  1714. std::string* why);
  1715. // Iterate over global symbols, calling a visitor class V for each.
  1716. void
  1717. do_for_all_global_symbols(Read_symbols_data* sd,
  1718. Library_base::Symbol_visitor_base* v);
  1719. // Iterate over local symbols, calling a visitor class V for each GOT offset
  1720. // associated with a local symbol.
  1721. void
  1722. do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
  1723. // Get the size of a section.
  1724. uint64_t
  1725. do_section_size(unsigned int shndx);
  1726. // Get the name of a section.
  1727. std::string
  1728. do_section_name(unsigned int shndx) const;
  1729. // Return a view of the contents of a section.
  1730. const unsigned char*
  1731. do_section_contents(unsigned int shndx, section_size_type* plen,
  1732. bool cache);
  1733. // Return section flags.
  1734. uint64_t
  1735. do_section_flags(unsigned int shndx);
  1736. // Return section entsize.
  1737. uint64_t
  1738. do_section_entsize(unsigned int shndx);
  1739. // Return section address.
  1740. uint64_t
  1741. do_section_address(unsigned int shndx);
  1742. // Return section type.
  1743. unsigned int
  1744. do_section_type(unsigned int shndx);
  1745. // Return the section link field.
  1746. unsigned int
  1747. do_section_link(unsigned int shndx);
  1748. // Return the section link field.
  1749. unsigned int
  1750. do_section_info(unsigned int shndx);
  1751. // Return the section alignment.
  1752. uint64_t
  1753. do_section_addralign(unsigned int shndx);
  1754. // Return the Xindex structure to use.
  1755. Xindex*
  1756. do_initialize_xindex();
  1757. // Get symbol counts.
  1758. void
  1759. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
  1760. // Get global symbols.
  1761. const Symbols*
  1762. do_get_global_symbols() const
  1763. { return &this->symbols_; }
  1764. // The Incremental_binary base file.
  1765. Sized_incremental_binary<size, big_endian>* ibase_;
  1766. // The index of the object in the input file list.
  1767. unsigned int input_file_index_;
  1768. // The reader for the input file.
  1769. Input_entry_reader input_reader_;
  1770. // The entries in the symbol table for the external symbols.
  1771. Symbols symbols_;
  1772. // Number of symbols defined in object file itself.
  1773. size_t defined_count_;
  1774. };
  1775. // Allocate an incremental object of the appropriate size and endianness.
  1776. extern Object*
  1777. make_sized_incremental_object(
  1778. Incremental_binary* base,
  1779. unsigned int input_file_index,
  1780. Incremental_input_type input_type,
  1781. const Incremental_binary::Input_reader* input_reader);
  1782. // This class represents an Archive library (or --start-lib/--end-lib group)
  1783. // that has not changed since the last incremental link. Its contents come
  1784. // from the incremental inputs entry in the base file.
  1785. class Incremental_library : public Library_base
  1786. {
  1787. public:
  1788. Incremental_library(const char* filename, unsigned int input_file_index,
  1789. const Incremental_binary::Input_reader* input_reader)
  1790. : Library_base(NULL), filename_(filename),
  1791. input_file_index_(input_file_index), input_reader_(input_reader),
  1792. unused_symbols_(), is_reported_(false)
  1793. { }
  1794. // Return the input file index.
  1795. unsigned int
  1796. input_file_index() const
  1797. { return this->input_file_index_; }
  1798. // Return the serial number of the input file.
  1799. unsigned int
  1800. arg_serial() const
  1801. { return this->input_reader_->arg_serial(); }
  1802. // Copy the unused symbols from the incremental input info.
  1803. // We need to do this because we may be overwriting the incremental
  1804. // input info in the base file before we write the new incremental
  1805. // info.
  1806. void
  1807. copy_unused_symbols();
  1808. // Return FALSE on the first call to indicate that the library needs
  1809. // to be recorded; return TRUE subsequently.
  1810. bool
  1811. is_reported()
  1812. {
  1813. bool was_reported = this->is_reported_;
  1814. is_reported_ = true;
  1815. return was_reported;
  1816. }
  1817. private:
  1818. typedef std::vector<std::string> Symbol_list;
  1819. // The file name.
  1820. const std::string&
  1821. do_filename() const
  1822. { return this->filename_; }
  1823. // Return the modification time of the archive file.
  1824. Timespec
  1825. do_get_mtime()
  1826. { return this->input_reader_->get_mtime(); }
  1827. // Iterator for unused global symbols in the library.
  1828. void
  1829. do_for_all_unused_symbols(Symbol_visitor_base* v) const;
  1830. // The name of the library.
  1831. std::string filename_;
  1832. // The input file index of this library.
  1833. unsigned int input_file_index_;
  1834. // A reader for the incremental input information.
  1835. const Incremental_binary::Input_reader* input_reader_;
  1836. // List of unused symbols defined in this library.
  1837. Symbol_list unused_symbols_;
  1838. // TRUE when this library has been reported to the new incremental info.
  1839. bool is_reported_;
  1840. };
  1841. } // End namespace gold.
  1842. #endif // !defined(GOLD_INCREMENTAL_H)