incremental.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. // inremental.h -- incremental linking support for gold -*- C++ -*-
  2. // Copyright (C) 2009-2022 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. Input_reader(const Input_reader&)
  1133. { }
  1134. virtual
  1135. ~Input_reader()
  1136. { }
  1137. const char*
  1138. filename() const
  1139. { return this->do_filename(); }
  1140. Timespec
  1141. get_mtime() const
  1142. { return this->do_get_mtime(); }
  1143. Incremental_input_type
  1144. type() const
  1145. { return this->do_type(); }
  1146. unsigned int
  1147. arg_serial() const
  1148. { return this->do_arg_serial(); }
  1149. unsigned int
  1150. get_unused_symbol_count() const
  1151. { return this->do_get_unused_symbol_count(); }
  1152. const char*
  1153. get_unused_symbol(unsigned int n) const
  1154. { return this->do_get_unused_symbol(n); }
  1155. protected:
  1156. virtual const char*
  1157. do_filename() const = 0;
  1158. virtual Timespec
  1159. do_get_mtime() const = 0;
  1160. virtual Incremental_input_type
  1161. do_type() const = 0;
  1162. virtual unsigned int
  1163. do_arg_serial() const = 0;
  1164. virtual unsigned int
  1165. do_get_unused_symbol_count() const = 0;
  1166. virtual const char*
  1167. do_get_unused_symbol(unsigned int n) const = 0;
  1168. };
  1169. // Return the number of input files.
  1170. unsigned int
  1171. input_file_count() const
  1172. { return this->do_input_file_count(); }
  1173. // Return an Input_reader for input file N.
  1174. const Input_reader*
  1175. get_input_reader(unsigned int n) const
  1176. { return this->do_get_input_reader(n); }
  1177. // Return TRUE if the input file N has changed since the last link.
  1178. bool
  1179. file_has_changed(unsigned int n) const
  1180. { return this->do_file_has_changed(n); }
  1181. // Return the Input_argument for input file N. Returns NULL if
  1182. // the Input_argument is not available.
  1183. const Input_argument*
  1184. get_input_argument(unsigned int n) const
  1185. {
  1186. const Input_reader* input_file = this->do_get_input_reader(n);
  1187. unsigned int arg_serial = input_file->arg_serial();
  1188. if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
  1189. return NULL;
  1190. return this->input_args_map_[arg_serial - 1];
  1191. }
  1192. // Return an Incremental_library for the given input file.
  1193. Incremental_library*
  1194. get_library(unsigned int n) const
  1195. { return this->library_map_[n]; }
  1196. // Return a Script_info for the given input file.
  1197. Script_info*
  1198. get_script_info(unsigned int n) const
  1199. { return this->script_map_[n]; }
  1200. // Initialize the layout of the output file based on the existing
  1201. // output file.
  1202. void
  1203. init_layout(Layout* layout)
  1204. { this->do_init_layout(layout); }
  1205. // Mark regions of the input file that must be kept unchanged.
  1206. void
  1207. reserve_layout(unsigned int input_file_index)
  1208. { this->do_reserve_layout(input_file_index); }
  1209. // Process the GOT and PLT entries from the existing output file.
  1210. void
  1211. process_got_plt(Symbol_table* symtab, Layout* layout)
  1212. { this->do_process_got_plt(symtab, layout); }
  1213. // Emit COPY relocations from the existing output file.
  1214. void
  1215. emit_copy_relocs(Symbol_table* symtab)
  1216. { this->do_emit_copy_relocs(symtab); }
  1217. // Apply incremental relocations for symbols whose values have changed.
  1218. void
  1219. apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
  1220. Output_file* of)
  1221. { this->do_apply_incremental_relocs(symtab, layout, of); }
  1222. // Functions and types for the elfcpp::Elf_file interface. This
  1223. // permit us to use Incremental_binary as the File template parameter for
  1224. // elfcpp::Elf_file.
  1225. // The View class is returned by view. It must support a single
  1226. // method, data(). This is trivial, because Output_file::get_output_view
  1227. // does what we need.
  1228. class View
  1229. {
  1230. public:
  1231. View(const unsigned char* p)
  1232. : p_(p)
  1233. { }
  1234. const unsigned char*
  1235. data() const
  1236. { return this->p_; }
  1237. private:
  1238. const unsigned char* p_;
  1239. };
  1240. // Return a View.
  1241. View
  1242. view(off_t file_offset, section_size_type data_size)
  1243. { return View(this->output_->get_input_view(file_offset, data_size)); }
  1244. // A location in the file.
  1245. struct Location
  1246. {
  1247. off_t file_offset;
  1248. off_t data_size;
  1249. Location(off_t fo, section_size_type ds)
  1250. : file_offset(fo), data_size(ds)
  1251. { }
  1252. Location()
  1253. : file_offset(0), data_size(0)
  1254. { }
  1255. };
  1256. // Get a View given a Location.
  1257. View
  1258. view(Location loc)
  1259. { return View(this->view(loc.file_offset, loc.data_size)); }
  1260. // Return the Output_file.
  1261. Output_file*
  1262. output_file()
  1263. { return this->output_; }
  1264. protected:
  1265. // Check the .gnu_incremental_inputs section to see whether an incremental
  1266. // build is possible.
  1267. virtual bool
  1268. do_check_inputs(const Command_line& cmdline,
  1269. Incremental_inputs* incremental_inputs) = 0;
  1270. // Return TRUE if input file N has changed since the last incremental link.
  1271. virtual bool
  1272. do_file_has_changed(unsigned int n) const = 0;
  1273. // Initialize the layout of the output file based on the existing
  1274. // output file.
  1275. virtual void
  1276. do_init_layout(Layout* layout) = 0;
  1277. // Mark regions of the input file that must be kept unchanged.
  1278. virtual void
  1279. do_reserve_layout(unsigned int input_file_index) = 0;
  1280. // Process the GOT and PLT entries from the existing output file.
  1281. virtual void
  1282. do_process_got_plt(Symbol_table* symtab, Layout* layout) = 0;
  1283. // Emit COPY relocations from the existing output file.
  1284. virtual void
  1285. do_emit_copy_relocs(Symbol_table* symtab) = 0;
  1286. // Apply incremental relocations for symbols whose values have changed.
  1287. virtual void
  1288. do_apply_incremental_relocs(const Symbol_table*, Layout*, Output_file*) = 0;
  1289. virtual unsigned int
  1290. do_input_file_count() const = 0;
  1291. virtual const Input_reader*
  1292. do_get_input_reader(unsigned int) const = 0;
  1293. // Map from input file index to Input_argument.
  1294. std::vector<const Input_argument*> input_args_map_;
  1295. // Map from an input file index to an Incremental_library.
  1296. std::vector<Incremental_library*> library_map_;
  1297. // Map from an input file index to a Script_info.
  1298. std::vector<Script_info*> script_map_;
  1299. private:
  1300. // Edited output file object.
  1301. Output_file* output_;
  1302. };
  1303. template<int size, bool big_endian>
  1304. class Sized_relobj_incr;
  1305. template<int size, bool big_endian>
  1306. class Sized_incremental_binary : public Incremental_binary
  1307. {
  1308. public:
  1309. Sized_incremental_binary(Output_file* output,
  1310. const elfcpp::Ehdr<size, big_endian>& ehdr,
  1311. Target* target)
  1312. : Incremental_binary(output, target), elf_file_(this, ehdr),
  1313. input_objects_(), section_map_(), symbol_map_(), copy_relocs_(),
  1314. main_symtab_loc_(), main_strtab_loc_(), has_incremental_info_(false),
  1315. inputs_reader_(), symtab_reader_(), relocs_reader_(), got_plt_reader_(),
  1316. input_entry_readers_()
  1317. { this->setup_readers(); }
  1318. // Returns TRUE if the file contains incremental info.
  1319. bool
  1320. has_incremental_info() const
  1321. { return this->has_incremental_info_; }
  1322. // Record a pointer to the object for input file N.
  1323. void
  1324. set_input_object(unsigned int n,
  1325. Sized_relobj_incr<size, big_endian>* obj)
  1326. { this->input_objects_[n] = obj; }
  1327. // Return a pointer to the object for input file N.
  1328. Sized_relobj_incr<size, big_endian>*
  1329. input_object(unsigned int n) const
  1330. {
  1331. gold_assert(n < this->input_objects_.size());
  1332. return this->input_objects_[n];
  1333. }
  1334. // Return the Output_section for section index SHNDX.
  1335. Output_section*
  1336. output_section(unsigned int shndx)
  1337. { return this->section_map_[shndx]; }
  1338. // Map a symbol table entry from the base file to the output symbol table.
  1339. // SYMNDX is relative to the first forced-local or global symbol in the
  1340. // input file symbol table.
  1341. void
  1342. add_global_symbol(unsigned int symndx, Symbol* gsym)
  1343. { this->symbol_map_[symndx] = gsym; }
  1344. // Map a symbol table entry from the base file to the output symbol table.
  1345. // SYMNDX is relative to the first forced-local or global symbol in the
  1346. // input file symbol table.
  1347. Symbol*
  1348. global_symbol(unsigned int symndx) const
  1349. { return this->symbol_map_[symndx]; }
  1350. // Add a COPY relocation for a global symbol.
  1351. void
  1352. add_copy_reloc(Symbol* gsym, Output_section* os, off_t offset)
  1353. { this->copy_relocs_.push_back(Copy_reloc(gsym, os, offset)); }
  1354. // Readers for the incremental info sections.
  1355. const Incremental_inputs_reader<size, big_endian>&
  1356. inputs_reader() const
  1357. { return this->inputs_reader_; }
  1358. const Incremental_symtab_reader<big_endian>&
  1359. symtab_reader() const
  1360. { return this->symtab_reader_; }
  1361. const Incremental_relocs_reader<size, big_endian>&
  1362. relocs_reader() const
  1363. { return this->relocs_reader_; }
  1364. const Incremental_got_plt_reader<big_endian>&
  1365. got_plt_reader() const
  1366. { return this->got_plt_reader_; }
  1367. void
  1368. get_symtab_view(View* symtab_view, unsigned int* sym_count,
  1369. elfcpp::Elf_strtab* strtab);
  1370. protected:
  1371. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  1372. typedef typename Inputs_reader::Incremental_input_entry_reader
  1373. Input_entry_reader;
  1374. virtual bool
  1375. do_check_inputs(const Command_line& cmdline,
  1376. Incremental_inputs* incremental_inputs);
  1377. // Return TRUE if input file N has changed since the last incremental link.
  1378. virtual bool
  1379. do_file_has_changed(unsigned int n) const;
  1380. // Initialize the layout of the output file based on the existing
  1381. // output file.
  1382. virtual void
  1383. do_init_layout(Layout* layout);
  1384. // Mark regions of the input file that must be kept unchanged.
  1385. virtual void
  1386. do_reserve_layout(unsigned int input_file_index);
  1387. // Process the GOT and PLT entries from the existing output file.
  1388. virtual void
  1389. do_process_got_plt(Symbol_table* symtab, Layout* layout);
  1390. // Emit COPY relocations from the existing output file.
  1391. virtual void
  1392. do_emit_copy_relocs(Symbol_table* symtab);
  1393. // Apply incremental relocations for symbols whose values have changed.
  1394. virtual void
  1395. do_apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
  1396. Output_file* of);
  1397. // Proxy class for a sized Incremental_input_entry_reader.
  1398. class Sized_input_reader : public Input_reader
  1399. {
  1400. public:
  1401. Sized_input_reader(Input_entry_reader r)
  1402. : Input_reader(), reader_(r)
  1403. { }
  1404. Sized_input_reader(const Sized_input_reader& r)
  1405. : Input_reader(), reader_(r.reader_)
  1406. { }
  1407. virtual
  1408. ~Sized_input_reader()
  1409. { }
  1410. private:
  1411. const char*
  1412. do_filename() const
  1413. { return this->reader_.filename(); }
  1414. Timespec
  1415. do_get_mtime() const
  1416. { return this->reader_.get_mtime(); }
  1417. Incremental_input_type
  1418. do_type() const
  1419. { return this->reader_.type(); }
  1420. unsigned int
  1421. do_arg_serial() const
  1422. { return this->reader_.arg_serial(); }
  1423. unsigned int
  1424. do_get_unused_symbol_count() const
  1425. { return this->reader_.get_unused_symbol_count(); }
  1426. const char*
  1427. do_get_unused_symbol(unsigned int n) const
  1428. { return this->reader_.get_unused_symbol(n); }
  1429. Input_entry_reader reader_;
  1430. };
  1431. virtual unsigned int
  1432. do_input_file_count() const
  1433. { return this->inputs_reader_.input_file_count(); }
  1434. virtual const Input_reader*
  1435. do_get_input_reader(unsigned int n) const
  1436. {
  1437. gold_assert(n < this->input_entry_readers_.size());
  1438. return &this->input_entry_readers_[n];
  1439. }
  1440. private:
  1441. // List of symbols that need COPY relocations.
  1442. struct Copy_reloc
  1443. {
  1444. Copy_reloc(Symbol* sym, Output_section* os, off_t off)
  1445. : symbol(sym), output_section(os), offset(off)
  1446. { }
  1447. // The global symbol to copy.
  1448. Symbol* symbol;
  1449. // The output section into which the symbol was copied.
  1450. Output_section* output_section;
  1451. // The offset within that output section.
  1452. off_t offset;
  1453. };
  1454. typedef std::vector<Copy_reloc> Copy_relocs;
  1455. bool
  1456. find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
  1457. unsigned int* p_symtab_shndx,
  1458. unsigned int* p_relocs_shndx,
  1459. unsigned int* p_got_plt_shndx,
  1460. unsigned int* p_strtab_shndx);
  1461. void
  1462. setup_readers();
  1463. // Output as an ELF file.
  1464. elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
  1465. // Vector of pointers to the input objects for the unchanged files.
  1466. // For replaced files, the corresponding pointer is NULL.
  1467. std::vector<Sized_relobj_incr<size, big_endian>*> input_objects_;
  1468. // Map section index to an Output_section in the updated layout.
  1469. std::vector<Output_section*> section_map_;
  1470. // Map global symbols from the input file to the symbol table.
  1471. std::vector<Symbol*> symbol_map_;
  1472. // List of symbols that need COPY relocations.
  1473. Copy_relocs copy_relocs_;
  1474. // Locations of the main symbol table and symbol string table.
  1475. Location main_symtab_loc_;
  1476. Location main_strtab_loc_;
  1477. // Readers for the incremental info sections.
  1478. bool has_incremental_info_;
  1479. Incremental_inputs_reader<size, big_endian> inputs_reader_;
  1480. Incremental_symtab_reader<big_endian> symtab_reader_;
  1481. Incremental_relocs_reader<size, big_endian> relocs_reader_;
  1482. Incremental_got_plt_reader<big_endian> got_plt_reader_;
  1483. std::vector<Sized_input_reader> input_entry_readers_;
  1484. };
  1485. // An incremental Relobj. This class represents a relocatable object
  1486. // that has not changed since the last incremental link, and whose contents
  1487. // can be used directly from the base file.
  1488. template<int size, bool big_endian>
  1489. class Sized_relobj_incr : public Sized_relobj<size, big_endian>
  1490. {
  1491. public:
  1492. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1493. typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
  1494. Sized_relobj_incr(const std::string& name,
  1495. Sized_incremental_binary<size, big_endian>* ibase,
  1496. unsigned int input_file_index);
  1497. private:
  1498. // For convenience.
  1499. typedef Sized_relobj_incr<size, big_endian> This;
  1500. static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1501. typedef typename Sized_relobj<size, big_endian>::Output_sections
  1502. Output_sections;
  1503. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  1504. typedef typename Inputs_reader::Incremental_input_entry_reader
  1505. Input_entry_reader;
  1506. // A local symbol.
  1507. struct Local_symbol
  1508. {
  1509. Local_symbol(const char* name_, Address value_, unsigned int size_,
  1510. unsigned int shndx_, unsigned int type_,
  1511. bool needs_dynsym_entry_)
  1512. : st_value(value_), name(name_), st_size(size_), st_shndx(shndx_),
  1513. st_type(type_), output_dynsym_index(0),
  1514. needs_dynsym_entry(needs_dynsym_entry_)
  1515. { }
  1516. // The symbol value.
  1517. Address st_value;
  1518. // The symbol name. This points to the stringpool entry.
  1519. const char* name;
  1520. // The symbol size.
  1521. unsigned int st_size;
  1522. // The output section index.
  1523. unsigned int st_shndx : 28;
  1524. // The symbol type.
  1525. unsigned int st_type : 4;
  1526. // The index of the symbol in the output dynamic symbol table.
  1527. unsigned int output_dynsym_index : 31;
  1528. // TRUE if the symbol needs to appear in the dynamic symbol table.
  1529. unsigned int needs_dynsym_entry : 1;
  1530. };
  1531. // Return TRUE if this is an incremental (unchanged) input file.
  1532. bool
  1533. do_is_incremental() const
  1534. { return true; }
  1535. // Return the last modified time of the file.
  1536. Timespec
  1537. do_get_mtime()
  1538. { return this->input_reader_.get_mtime(); }
  1539. // Read the symbols.
  1540. void
  1541. do_read_symbols(Read_symbols_data*);
  1542. // Lay out the input sections.
  1543. void
  1544. do_layout(Symbol_table*, Layout*, Read_symbols_data*);
  1545. // Layout sections whose layout was deferred while waiting for
  1546. // input files from a plugin.
  1547. void
  1548. do_layout_deferred_sections(Layout*);
  1549. // Add the symbols to the symbol table.
  1550. void
  1551. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
  1552. Archive::Should_include
  1553. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  1554. std::string* why);
  1555. // Iterate over global symbols, calling a visitor class V for each.
  1556. void
  1557. do_for_all_global_symbols(Read_symbols_data* sd,
  1558. Library_base::Symbol_visitor_base* v);
  1559. // Get the size of a section.
  1560. uint64_t
  1561. do_section_size(unsigned int shndx);
  1562. // Get the name of a section.
  1563. std::string
  1564. do_section_name(unsigned int shndx) const;
  1565. // Return a view of the contents of a section.
  1566. const unsigned char*
  1567. do_section_contents(unsigned int shndx, section_size_type* plen,
  1568. bool cache);
  1569. // Return section flags.
  1570. uint64_t
  1571. do_section_flags(unsigned int shndx);
  1572. // Return section entsize.
  1573. uint64_t
  1574. do_section_entsize(unsigned int shndx);
  1575. // Return section address.
  1576. uint64_t
  1577. do_section_address(unsigned int shndx);
  1578. // Return section type.
  1579. unsigned int
  1580. do_section_type(unsigned int shndx);
  1581. // Return the section link field.
  1582. unsigned int
  1583. do_section_link(unsigned int shndx);
  1584. // Return the section link field.
  1585. unsigned int
  1586. do_section_info(unsigned int shndx);
  1587. // Return the section alignment.
  1588. uint64_t
  1589. do_section_addralign(unsigned int shndx);
  1590. // Return the Xindex structure to use.
  1591. Xindex*
  1592. do_initialize_xindex();
  1593. // Get symbol counts.
  1594. void
  1595. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
  1596. // Get global symbols.
  1597. const Symbols*
  1598. do_get_global_symbols() const
  1599. { return &this->symbols_; }
  1600. // Return the value of a local symbol.
  1601. uint64_t
  1602. do_local_symbol_value(unsigned int, uint64_t) const
  1603. { gold_unreachable(); }
  1604. unsigned int
  1605. do_local_plt_offset(unsigned int) const
  1606. { gold_unreachable(); }
  1607. bool
  1608. do_local_is_tls(unsigned int) const
  1609. { gold_unreachable(); }
  1610. // Return the number of local symbols.
  1611. unsigned int
  1612. do_local_symbol_count() const
  1613. { return this->local_symbol_count_; }
  1614. // Return the number of local symbols in the output symbol table.
  1615. unsigned int
  1616. do_output_local_symbol_count() const
  1617. { return this->local_symbol_count_; }
  1618. // Return the file offset for local symbols in the output symbol table.
  1619. off_t
  1620. do_local_symbol_offset() const
  1621. { return this->local_symbol_offset_; }
  1622. // Read the relocs.
  1623. void
  1624. do_read_relocs(Read_relocs_data*);
  1625. // Process the relocs to find list of referenced sections. Used only
  1626. // during garbage collection.
  1627. void
  1628. do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  1629. // Scan the relocs and adjust the symbol table.
  1630. void
  1631. do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  1632. // Count the local symbols.
  1633. void
  1634. do_count_local_symbols(Stringpool_template<char>*,
  1635. Stringpool_template<char>*);
  1636. // Finalize the local symbols.
  1637. unsigned int
  1638. do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
  1639. // Set the offset where local dynamic symbol information will be stored.
  1640. unsigned int
  1641. do_set_local_dynsym_indexes(unsigned int);
  1642. // Set the offset where local dynamic symbol information will be stored.
  1643. unsigned int
  1644. do_set_local_dynsym_offset(off_t);
  1645. // Relocate the input sections and write out the local symbols.
  1646. void
  1647. do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
  1648. // Set the offset of a section.
  1649. void
  1650. do_set_section_offset(unsigned int shndx, uint64_t off);
  1651. // The Incremental_binary base file.
  1652. Sized_incremental_binary<size, big_endian>* ibase_;
  1653. // The index of the object in the input file list.
  1654. unsigned int input_file_index_;
  1655. // The reader for the input file.
  1656. Input_entry_reader input_reader_;
  1657. // The number of local symbols.
  1658. unsigned int local_symbol_count_;
  1659. // The number of local symbols which go into the output file's dynamic
  1660. // symbol table.
  1661. unsigned int output_local_dynsym_count_;
  1662. // This starting symbol index in the output symbol table.
  1663. unsigned int local_symbol_index_;
  1664. // The file offset for local symbols in the output symbol table.
  1665. unsigned int local_symbol_offset_;
  1666. // The file offset for local symbols in the output symbol table.
  1667. unsigned int local_dynsym_offset_;
  1668. // The entries in the symbol table for the external symbols.
  1669. Symbols symbols_;
  1670. // Number of symbols defined in object file itself.
  1671. size_t defined_count_;
  1672. // The offset of the first incremental relocation for this object.
  1673. unsigned int incr_reloc_offset_;
  1674. // The number of incremental relocations for this object.
  1675. unsigned int incr_reloc_count_;
  1676. // The index of the first incremental relocation for this object in the
  1677. // updated output file.
  1678. unsigned int incr_reloc_output_index_;
  1679. // A copy of the incremental relocations from this object.
  1680. unsigned char* incr_relocs_;
  1681. // The local symbols.
  1682. std::vector<Local_symbol> local_symbols_;
  1683. };
  1684. // An incremental Dynobj. This class represents a shared object that has
  1685. // not changed since the last incremental link, and whose contents can be
  1686. // used directly from the base file.
  1687. template<int size, bool big_endian>
  1688. class Sized_incr_dynobj : public Dynobj
  1689. {
  1690. public:
  1691. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1692. static const Address invalid_address = static_cast<Address>(0) - 1;
  1693. Sized_incr_dynobj(const std::string& name,
  1694. Sized_incremental_binary<size, big_endian>* ibase,
  1695. unsigned int input_file_index);
  1696. private:
  1697. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  1698. typedef typename Inputs_reader::Incremental_input_entry_reader
  1699. Input_entry_reader;
  1700. // Return TRUE if this is an incremental (unchanged) input file.
  1701. bool
  1702. do_is_incremental() const
  1703. { return true; }
  1704. // Return the last modified time of the file.
  1705. Timespec
  1706. do_get_mtime()
  1707. { return this->input_reader_.get_mtime(); }
  1708. // Read the symbols.
  1709. void
  1710. do_read_symbols(Read_symbols_data*);
  1711. // Lay out the input sections.
  1712. void
  1713. do_layout(Symbol_table*, Layout*, Read_symbols_data*);
  1714. // Add the symbols to the symbol table.
  1715. void
  1716. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
  1717. Archive::Should_include
  1718. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  1719. std::string* why);
  1720. // Iterate over global symbols, calling a visitor class V for each.
  1721. void
  1722. do_for_all_global_symbols(Read_symbols_data* sd,
  1723. Library_base::Symbol_visitor_base* v);
  1724. // Iterate over local symbols, calling a visitor class V for each GOT offset
  1725. // associated with a local symbol.
  1726. void
  1727. do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
  1728. // Get the size of a section.
  1729. uint64_t
  1730. do_section_size(unsigned int shndx);
  1731. // Get the name of a section.
  1732. std::string
  1733. do_section_name(unsigned int shndx) const;
  1734. // Return a view of the contents of a section.
  1735. const unsigned char*
  1736. do_section_contents(unsigned int shndx, section_size_type* plen,
  1737. bool cache);
  1738. // Return section flags.
  1739. uint64_t
  1740. do_section_flags(unsigned int shndx);
  1741. // Return section entsize.
  1742. uint64_t
  1743. do_section_entsize(unsigned int shndx);
  1744. // Return section address.
  1745. uint64_t
  1746. do_section_address(unsigned int shndx);
  1747. // Return section type.
  1748. unsigned int
  1749. do_section_type(unsigned int shndx);
  1750. // Return the section link field.
  1751. unsigned int
  1752. do_section_link(unsigned int shndx);
  1753. // Return the section link field.
  1754. unsigned int
  1755. do_section_info(unsigned int shndx);
  1756. // Return the section alignment.
  1757. uint64_t
  1758. do_section_addralign(unsigned int shndx);
  1759. // Return the Xindex structure to use.
  1760. Xindex*
  1761. do_initialize_xindex();
  1762. // Get symbol counts.
  1763. void
  1764. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
  1765. // Get global symbols.
  1766. const Symbols*
  1767. do_get_global_symbols() const
  1768. { return &this->symbols_; }
  1769. // The Incremental_binary base file.
  1770. Sized_incremental_binary<size, big_endian>* ibase_;
  1771. // The index of the object in the input file list.
  1772. unsigned int input_file_index_;
  1773. // The reader for the input file.
  1774. Input_entry_reader input_reader_;
  1775. // The entries in the symbol table for the external symbols.
  1776. Symbols symbols_;
  1777. // Number of symbols defined in object file itself.
  1778. size_t defined_count_;
  1779. };
  1780. // Allocate an incremental object of the appropriate size and endianness.
  1781. extern Object*
  1782. make_sized_incremental_object(
  1783. Incremental_binary* base,
  1784. unsigned int input_file_index,
  1785. Incremental_input_type input_type,
  1786. const Incremental_binary::Input_reader* input_reader);
  1787. // This class represents an Archive library (or --start-lib/--end-lib group)
  1788. // that has not changed since the last incremental link. Its contents come
  1789. // from the incremental inputs entry in the base file.
  1790. class Incremental_library : public Library_base
  1791. {
  1792. public:
  1793. Incremental_library(const char* filename, unsigned int input_file_index,
  1794. const Incremental_binary::Input_reader* input_reader)
  1795. : Library_base(NULL), filename_(filename),
  1796. input_file_index_(input_file_index), input_reader_(input_reader),
  1797. unused_symbols_(), is_reported_(false)
  1798. { }
  1799. // Return the input file index.
  1800. unsigned int
  1801. input_file_index() const
  1802. { return this->input_file_index_; }
  1803. // Return the serial number of the input file.
  1804. unsigned int
  1805. arg_serial() const
  1806. { return this->input_reader_->arg_serial(); }
  1807. // Copy the unused symbols from the incremental input info.
  1808. // We need to do this because we may be overwriting the incremental
  1809. // input info in the base file before we write the new incremental
  1810. // info.
  1811. void
  1812. copy_unused_symbols();
  1813. // Return FALSE on the first call to indicate that the library needs
  1814. // to be recorded; return TRUE subsequently.
  1815. bool
  1816. is_reported()
  1817. {
  1818. bool was_reported = this->is_reported_;
  1819. is_reported_ = true;
  1820. return was_reported;
  1821. }
  1822. private:
  1823. typedef std::vector<std::string> Symbol_list;
  1824. // The file name.
  1825. const std::string&
  1826. do_filename() const
  1827. { return this->filename_; }
  1828. // Return the modification time of the archive file.
  1829. Timespec
  1830. do_get_mtime()
  1831. { return this->input_reader_->get_mtime(); }
  1832. // Iterator for unused global symbols in the library.
  1833. void
  1834. do_for_all_unused_symbols(Symbol_visitor_base* v) const;
  1835. // The name of the library.
  1836. std::string filename_;
  1837. // The input file index of this library.
  1838. unsigned int input_file_index_;
  1839. // A reader for the incremental input information.
  1840. const Incremental_binary::Input_reader* input_reader_;
  1841. // List of unused symbols defined in this library.
  1842. Symbol_list unused_symbols_;
  1843. // TRUE when this library has been reported to the new incremental info.
  1844. bool is_reported_;
  1845. };
  1846. } // End namespace gold.
  1847. #endif // !defined(GOLD_INCREMENTAL_H)