incremental.cc 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123
  1. // inremental.cc -- incremental linking support for gold
  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. #include "gold.h"
  18. #include <set>
  19. #include <cstdarg>
  20. #include "libiberty.h"
  21. #include "elfcpp.h"
  22. #include "options.h"
  23. #include "output.h"
  24. #include "symtab.h"
  25. #include "incremental.h"
  26. #include "archive.h"
  27. #include "object.h"
  28. #include "target-select.h"
  29. #include "target.h"
  30. #include "fileread.h"
  31. #include "script.h"
  32. namespace gold {
  33. // Version number for the .gnu_incremental_inputs section.
  34. // Version 1 was the initial checkin.
  35. // Version 2 adds some padding to ensure 8-byte alignment where necessary.
  36. const unsigned int INCREMENTAL_LINK_VERSION = 2;
  37. // This class manages the .gnu_incremental_inputs section, which holds
  38. // the header information, a directory of input files, and separate
  39. // entries for each input file.
  40. template<int size, bool big_endian>
  41. class Output_section_incremental_inputs : public Output_section_data
  42. {
  43. public:
  44. Output_section_incremental_inputs(const Incremental_inputs* inputs,
  45. const Symbol_table* symtab)
  46. : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
  47. { }
  48. protected:
  49. // This is called to update the section size prior to assigning
  50. // the address and file offset.
  51. void
  52. update_data_size()
  53. { this->set_final_data_size(); }
  54. // Set the final data size.
  55. void
  56. set_final_data_size();
  57. // Write the data to the file.
  58. void
  59. do_write(Output_file*);
  60. // Write to a map file.
  61. void
  62. do_print_to_mapfile(Mapfile* mapfile) const
  63. { mapfile->print_output_data(this, _("** incremental_inputs")); }
  64. private:
  65. // Write the section header.
  66. unsigned char*
  67. write_header(unsigned char* pov, unsigned int input_file_count,
  68. section_offset_type command_line_offset);
  69. // Write the input file entries.
  70. unsigned char*
  71. write_input_files(unsigned char* oview, unsigned char* pov,
  72. Stringpool* strtab);
  73. // Write the supplemental information blocks.
  74. unsigned char*
  75. write_info_blocks(unsigned char* oview, unsigned char* pov,
  76. Stringpool* strtab, unsigned int* global_syms,
  77. unsigned int global_sym_count);
  78. // Write the contents of the .gnu_incremental_symtab section.
  79. void
  80. write_symtab(unsigned char* pov, unsigned int* global_syms,
  81. unsigned int global_sym_count);
  82. // Write the contents of the .gnu_incremental_got_plt section.
  83. void
  84. write_got_plt(unsigned char* pov, off_t view_size);
  85. // Typedefs for writing the data to the output sections.
  86. typedef elfcpp::Swap<size, big_endian> Swap;
  87. typedef elfcpp::Swap<16, big_endian> Swap16;
  88. typedef elfcpp::Swap<32, big_endian> Swap32;
  89. typedef elfcpp::Swap<64, big_endian> Swap64;
  90. // Sizes of various structures.
  91. static const int sizeof_addr = size / 8;
  92. static const int header_size =
  93. Incremental_inputs_reader<size, big_endian>::header_size;
  94. static const int input_entry_size =
  95. Incremental_inputs_reader<size, big_endian>::input_entry_size;
  96. static const unsigned int object_info_size =
  97. Incremental_inputs_reader<size, big_endian>::object_info_size;
  98. static const unsigned int input_section_entry_size =
  99. Incremental_inputs_reader<size, big_endian>::input_section_entry_size;
  100. static const unsigned int global_sym_entry_size =
  101. Incremental_inputs_reader<size, big_endian>::global_sym_entry_size;
  102. static const unsigned int incr_reloc_size =
  103. Incremental_relocs_reader<size, big_endian>::reloc_size;
  104. // The Incremental_inputs object.
  105. const Incremental_inputs* inputs_;
  106. // The symbol table.
  107. const Symbol_table* symtab_;
  108. };
  109. // Inform the user why we don't do an incremental link. Not called in
  110. // the obvious case of missing output file. TODO: Is this helpful?
  111. void
  112. vexplain_no_incremental(const char* format, va_list args)
  113. {
  114. char* buf = NULL;
  115. if (vasprintf(&buf, format, args) < 0)
  116. gold_nomem();
  117. gold_info(_("the link might take longer: "
  118. "cannot perform incremental link: %s"), buf);
  119. free(buf);
  120. }
  121. void
  122. explain_no_incremental(const char* format, ...)
  123. {
  124. va_list args;
  125. va_start(args, format);
  126. vexplain_no_incremental(format, args);
  127. va_end(args);
  128. }
  129. // Report an error.
  130. void
  131. Incremental_binary::error(const char* format, ...) const
  132. {
  133. va_list args;
  134. va_start(args, format);
  135. // Current code only checks if the file can be used for incremental linking,
  136. // so errors shouldn't fail the build, but only result in a fallback to a
  137. // full build.
  138. // TODO: when we implement incremental editing of the file, we may need a
  139. // flag that will cause errors to be treated seriously.
  140. vexplain_no_incremental(format, args);
  141. va_end(args);
  142. }
  143. // Return TRUE if a section of type SH_TYPE can be updated in place
  144. // during an incremental update. We can update sections of type PROGBITS,
  145. // NOBITS, INIT_ARRAY, FINI_ARRAY, PREINIT_ARRAY, and NOTE. All others
  146. // will be regenerated.
  147. bool
  148. can_incremental_update(unsigned int sh_type)
  149. {
  150. return (sh_type == elfcpp::SHT_PROGBITS
  151. || sh_type == elfcpp::SHT_NOBITS
  152. || sh_type == elfcpp::SHT_INIT_ARRAY
  153. || sh_type == elfcpp::SHT_FINI_ARRAY
  154. || sh_type == elfcpp::SHT_PREINIT_ARRAY
  155. || sh_type == elfcpp::SHT_NOTE);
  156. }
  157. // Find the .gnu_incremental_inputs section and related sections.
  158. template<int size, bool big_endian>
  159. bool
  160. Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
  161. unsigned int* p_inputs_shndx,
  162. unsigned int* p_symtab_shndx,
  163. unsigned int* p_relocs_shndx,
  164. unsigned int* p_got_plt_shndx,
  165. unsigned int* p_strtab_shndx)
  166. {
  167. unsigned int inputs_shndx =
  168. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
  169. if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found.
  170. return false;
  171. unsigned int symtab_shndx =
  172. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
  173. if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
  174. return false;
  175. if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
  176. return false;
  177. unsigned int relocs_shndx =
  178. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
  179. if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found.
  180. return false;
  181. if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
  182. return false;
  183. unsigned int got_plt_shndx =
  184. this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
  185. if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found.
  186. return false;
  187. if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
  188. return false;
  189. unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
  190. if (strtab_shndx == elfcpp::SHN_UNDEF
  191. || strtab_shndx > this->elf_file_.shnum()
  192. || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
  193. return false;
  194. if (p_inputs_shndx != NULL)
  195. *p_inputs_shndx = inputs_shndx;
  196. if (p_symtab_shndx != NULL)
  197. *p_symtab_shndx = symtab_shndx;
  198. if (p_relocs_shndx != NULL)
  199. *p_relocs_shndx = relocs_shndx;
  200. if (p_got_plt_shndx != NULL)
  201. *p_got_plt_shndx = got_plt_shndx;
  202. if (p_strtab_shndx != NULL)
  203. *p_strtab_shndx = strtab_shndx;
  204. return true;
  205. }
  206. // Set up the readers into the incremental info sections.
  207. template<int size, bool big_endian>
  208. void
  209. Sized_incremental_binary<size, big_endian>::setup_readers()
  210. {
  211. unsigned int inputs_shndx;
  212. unsigned int symtab_shndx;
  213. unsigned int relocs_shndx;
  214. unsigned int got_plt_shndx;
  215. unsigned int strtab_shndx;
  216. if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
  217. &relocs_shndx, &got_plt_shndx,
  218. &strtab_shndx))
  219. return;
  220. Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
  221. Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
  222. Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
  223. Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
  224. Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
  225. View inputs_view = this->view(inputs_location);
  226. View symtab_view = this->view(symtab_location);
  227. View relocs_view = this->view(relocs_location);
  228. View got_plt_view = this->view(got_plt_location);
  229. View strtab_view = this->view(strtab_location);
  230. elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
  231. this->inputs_reader_ =
  232. Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
  233. this->symtab_reader_ =
  234. Incremental_symtab_reader<big_endian>(symtab_view.data(),
  235. symtab_location.data_size);
  236. this->relocs_reader_ =
  237. Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
  238. relocs_location.data_size);
  239. this->got_plt_reader_ =
  240. Incremental_got_plt_reader<big_endian>(got_plt_view.data());
  241. // Find the main symbol table.
  242. unsigned int main_symtab_shndx =
  243. this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
  244. gold_assert(main_symtab_shndx != elfcpp::SHN_UNDEF);
  245. this->main_symtab_loc_ = this->elf_file_.section_contents(main_symtab_shndx);
  246. // Find the main symbol string table.
  247. unsigned int main_strtab_shndx =
  248. this->elf_file_.section_link(main_symtab_shndx);
  249. gold_assert(main_strtab_shndx != elfcpp::SHN_UNDEF
  250. && main_strtab_shndx < this->elf_file_.shnum());
  251. this->main_strtab_loc_ = this->elf_file_.section_contents(main_strtab_shndx);
  252. // Walk the list of input files (a) to setup an Input_reader for each
  253. // input file, and (b) to record maps of files added from archive
  254. // libraries and scripts.
  255. Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
  256. unsigned int count = inputs.input_file_count();
  257. this->input_objects_.resize(count);
  258. this->input_entry_readers_.reserve(count);
  259. this->library_map_.resize(count);
  260. this->script_map_.resize(count);
  261. for (unsigned int i = 0; i < count; i++)
  262. {
  263. Input_entry_reader input_file = inputs.input_file(i);
  264. this->input_entry_readers_.push_back(Sized_input_reader(input_file));
  265. switch (input_file.type())
  266. {
  267. case INCREMENTAL_INPUT_OBJECT:
  268. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  269. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  270. // No special treatment necessary.
  271. break;
  272. case INCREMENTAL_INPUT_ARCHIVE:
  273. {
  274. Incremental_library* lib =
  275. new Incremental_library(input_file.filename(), i,
  276. &this->input_entry_readers_[i]);
  277. this->library_map_[i] = lib;
  278. unsigned int member_count = input_file.get_member_count();
  279. for (unsigned int j = 0; j < member_count; j++)
  280. {
  281. int member_offset = input_file.get_member_offset(j);
  282. int member_index = inputs.input_file_index(member_offset);
  283. this->library_map_[member_index] = lib;
  284. }
  285. }
  286. break;
  287. case INCREMENTAL_INPUT_SCRIPT:
  288. {
  289. Script_info* script = new Script_info(input_file.filename(), i);
  290. this->script_map_[i] = script;
  291. unsigned int object_count = input_file.get_object_count();
  292. for (unsigned int j = 0; j < object_count; j++)
  293. {
  294. int object_offset = input_file.get_object_offset(j);
  295. int object_index = inputs.input_file_index(object_offset);
  296. this->script_map_[object_index] = script;
  297. }
  298. }
  299. break;
  300. default:
  301. gold_unreachable();
  302. }
  303. }
  304. // Initialize the map of global symbols.
  305. unsigned int nglobals = this->symtab_reader_.symbol_count();
  306. this->symbol_map_.resize(nglobals);
  307. this->has_incremental_info_ = true;
  308. }
  309. // Walk the list of input files given on the command line, and build
  310. // a direct map of file index to the corresponding input argument.
  311. void
  312. check_input_args(std::vector<const Input_argument*>& input_args_map,
  313. Input_arguments::const_iterator begin,
  314. Input_arguments::const_iterator end)
  315. {
  316. for (Input_arguments::const_iterator p = begin;
  317. p != end;
  318. ++p)
  319. {
  320. if (p->is_group())
  321. {
  322. const Input_file_group* group = p->group();
  323. check_input_args(input_args_map, group->begin(), group->end());
  324. }
  325. else if (p->is_lib())
  326. {
  327. const Input_file_lib* lib = p->lib();
  328. check_input_args(input_args_map, lib->begin(), lib->end());
  329. }
  330. else
  331. {
  332. gold_assert(p->is_file());
  333. unsigned int arg_serial = p->file().arg_serial();
  334. if (arg_serial > 0)
  335. {
  336. gold_assert(arg_serial <= input_args_map.size());
  337. gold_assert(input_args_map[arg_serial - 1] == 0);
  338. input_args_map[arg_serial - 1] = &*p;
  339. }
  340. }
  341. }
  342. }
  343. // Determine whether an incremental link based on the existing output file
  344. // can be done.
  345. template<int size, bool big_endian>
  346. bool
  347. Sized_incremental_binary<size, big_endian>::do_check_inputs(
  348. const Command_line& cmdline,
  349. Incremental_inputs* incremental_inputs)
  350. {
  351. Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
  352. if (!this->has_incremental_info_)
  353. {
  354. explain_no_incremental(_("no incremental data from previous build"));
  355. return false;
  356. }
  357. if (inputs.version() != INCREMENTAL_LINK_VERSION)
  358. {
  359. explain_no_incremental(_("different version of incremental build data"));
  360. return false;
  361. }
  362. if (incremental_inputs->command_line() != inputs.command_line())
  363. {
  364. gold_debug(DEBUG_INCREMENTAL,
  365. "old command line: %s",
  366. inputs.command_line());
  367. gold_debug(DEBUG_INCREMENTAL,
  368. "new command line: %s",
  369. incremental_inputs->command_line().c_str());
  370. explain_no_incremental(_("command line changed"));
  371. return false;
  372. }
  373. // Walk the list of input files given on the command line, and build
  374. // a direct map of argument serial numbers to the corresponding input
  375. // arguments.
  376. this->input_args_map_.resize(cmdline.number_of_input_files());
  377. check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
  378. // Walk the list of input files to check for conditions that prevent
  379. // an incremental update link.
  380. unsigned int count = inputs.input_file_count();
  381. for (unsigned int i = 0; i < count; i++)
  382. {
  383. Input_entry_reader input_file = inputs.input_file(i);
  384. switch (input_file.type())
  385. {
  386. case INCREMENTAL_INPUT_OBJECT:
  387. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  388. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  389. case INCREMENTAL_INPUT_ARCHIVE:
  390. // No special treatment necessary.
  391. break;
  392. case INCREMENTAL_INPUT_SCRIPT:
  393. if (this->do_file_has_changed(i))
  394. {
  395. explain_no_incremental(_("%s: script file changed"),
  396. input_file.filename());
  397. return false;
  398. }
  399. break;
  400. default:
  401. gold_unreachable();
  402. }
  403. }
  404. return true;
  405. }
  406. // Return TRUE if input file N has changed since the last incremental link.
  407. template<int size, bool big_endian>
  408. bool
  409. Sized_incremental_binary<size, big_endian>::do_file_has_changed(
  410. unsigned int n) const
  411. {
  412. Input_entry_reader input_file = this->inputs_reader_.input_file(n);
  413. Incremental_disposition disp = INCREMENTAL_CHECK;
  414. // For files named in scripts, find the file that was actually named
  415. // on the command line, so that we can get the incremental disposition
  416. // flag.
  417. Script_info* script = this->get_script_info(n);
  418. if (script != NULL)
  419. n = script->input_file_index();
  420. const Input_argument* input_argument = this->get_input_argument(n);
  421. if (input_argument != NULL)
  422. disp = input_argument->file().options().incremental_disposition();
  423. // For files at the beginning of the command line (i.e., those added
  424. // implicitly by gcc), check whether the --incremental-startup-unchanged
  425. // option was used.
  426. if (disp == INCREMENTAL_STARTUP)
  427. disp = parameters->options().incremental_startup_disposition();
  428. if (disp != INCREMENTAL_CHECK)
  429. return disp == INCREMENTAL_CHANGED;
  430. const char* filename = input_file.filename();
  431. Timespec old_mtime = input_file.get_mtime();
  432. Timespec new_mtime;
  433. if (!get_mtime(filename, &new_mtime))
  434. {
  435. // If we can't open get the current modification time, assume it has
  436. // changed. If the file doesn't exist, we'll issue an error when we
  437. // try to open it later.
  438. return true;
  439. }
  440. if (new_mtime.seconds > old_mtime.seconds)
  441. return true;
  442. if (new_mtime.seconds == old_mtime.seconds
  443. && new_mtime.nanoseconds > old_mtime.nanoseconds)
  444. return true;
  445. return false;
  446. }
  447. // Initialize the layout of the output file based on the existing
  448. // output file.
  449. template<int size, bool big_endian>
  450. void
  451. Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
  452. {
  453. typedef elfcpp::Shdr<size, big_endian> Shdr;
  454. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  455. // Get views of the section headers and the section string table.
  456. const off_t shoff = this->elf_file_.shoff();
  457. const unsigned int shnum = this->elf_file_.shnum();
  458. const unsigned int shstrndx = this->elf_file_.shstrndx();
  459. Location shdrs_location(shoff, shnum * shdr_size);
  460. Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
  461. View shdrs_view = this->view(shdrs_location);
  462. View shstrndx_view = this->view(shstrndx_location);
  463. elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
  464. shstrndx_location.data_size);
  465. layout->set_incremental_base(this);
  466. // Initialize the layout.
  467. this->section_map_.resize(shnum);
  468. const unsigned char* pshdr = shdrs_view.data() + shdr_size;
  469. for (unsigned int i = 1; i < shnum; i++)
  470. {
  471. Shdr shdr(pshdr);
  472. const char* name;
  473. if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
  474. name = NULL;
  475. gold_debug(DEBUG_INCREMENTAL,
  476. "Output section: %2d %08lx %08lx %08lx %3d %s",
  477. i,
  478. static_cast<long>(shdr.get_sh_addr()),
  479. static_cast<long>(shdr.get_sh_offset()),
  480. static_cast<long>(shdr.get_sh_size()),
  481. shdr.get_sh_type(), name ? name : "<null>");
  482. this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
  483. pshdr += shdr_size;
  484. }
  485. }
  486. // Mark regions of the input file that must be kept unchanged.
  487. template<int size, bool big_endian>
  488. void
  489. Sized_incremental_binary<size, big_endian>::do_reserve_layout(
  490. unsigned int input_file_index)
  491. {
  492. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  493. Input_entry_reader input_file =
  494. this->inputs_reader_.input_file(input_file_index);
  495. if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
  496. {
  497. // Reserve the BSS space used for COPY relocations.
  498. unsigned int nsyms = input_file.get_global_symbol_count();
  499. Incremental_binary::View symtab_view(NULL);
  500. unsigned int symtab_count;
  501. elfcpp::Elf_strtab strtab(NULL, 0);
  502. this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  503. for (unsigned int i = 0; i < nsyms; ++i)
  504. {
  505. bool is_def;
  506. bool is_copy;
  507. unsigned int output_symndx =
  508. input_file.get_output_symbol_index(i, &is_def, &is_copy);
  509. if (is_copy)
  510. {
  511. const unsigned char* sym_p = (symtab_view.data()
  512. + output_symndx * sym_size);
  513. elfcpp::Sym<size, big_endian> gsym(sym_p);
  514. unsigned int shndx = gsym.get_st_shndx();
  515. if (shndx < 1 || shndx >= this->section_map_.size())
  516. continue;
  517. Output_section* os = this->section_map_[shndx];
  518. off_t offset = gsym.get_st_value() - os->address();
  519. os->reserve(offset, gsym.get_st_size());
  520. gold_debug(DEBUG_INCREMENTAL,
  521. "Reserve for COPY reloc: %s, off %d, size %d",
  522. os->name(),
  523. static_cast<int>(offset),
  524. static_cast<int>(gsym.get_st_size()));
  525. }
  526. }
  527. return;
  528. }
  529. unsigned int shnum = input_file.get_input_section_count();
  530. for (unsigned int i = 0; i < shnum; i++)
  531. {
  532. typename Input_entry_reader::Input_section_info sect =
  533. input_file.get_input_section(i);
  534. if (sect.output_shndx == 0 || sect.sh_offset == -1)
  535. continue;
  536. Output_section* os = this->section_map_[sect.output_shndx];
  537. gold_assert(os != NULL);
  538. os->reserve(sect.sh_offset, sect.sh_size);
  539. }
  540. }
  541. // Process the GOT and PLT entries from the existing output file.
  542. template<int size, bool big_endian>
  543. void
  544. Sized_incremental_binary<size, big_endian>::do_process_got_plt(
  545. Symbol_table* symtab,
  546. Layout* layout)
  547. {
  548. Incremental_got_plt_reader<big_endian> got_plt_reader(this->got_plt_reader());
  549. Sized_target<size, big_endian>* target =
  550. parameters->sized_target<size, big_endian>();
  551. // Get the number of symbols in the main symbol table and in the
  552. // incremental symbol table. The difference between the two counts
  553. // is the index of the first forced-local or global symbol in the
  554. // main symbol table.
  555. unsigned int symtab_count =
  556. this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
  557. unsigned int isym_count = this->symtab_reader_.symbol_count();
  558. unsigned int first_global = symtab_count - isym_count;
  559. // Tell the target how big the GOT and PLT sections are.
  560. unsigned int got_count = got_plt_reader.get_got_entry_count();
  561. unsigned int plt_count = got_plt_reader.get_plt_entry_count();
  562. Output_data_got_base* got =
  563. target->init_got_plt_for_update(symtab, layout, got_count, plt_count);
  564. // Read the GOT entries from the base file and build the outgoing GOT.
  565. for (unsigned int i = 0; i < got_count; ++i)
  566. {
  567. unsigned int got_type = got_plt_reader.get_got_type(i);
  568. if ((got_type & 0x7f) == 0x7f)
  569. {
  570. // This is the second entry of a pair.
  571. got->reserve_slot(i);
  572. continue;
  573. }
  574. unsigned int symndx = got_plt_reader.get_got_symndx(i);
  575. if (got_type & 0x80)
  576. {
  577. // This is an entry for a local symbol. Ignore this entry if
  578. // the object file was replaced.
  579. unsigned int input_index = got_plt_reader.get_got_input_index(i);
  580. gold_debug(DEBUG_INCREMENTAL,
  581. "GOT entry %d, type %02x: (local symbol)",
  582. i, got_type & 0x7f);
  583. Sized_relobj_incr<size, big_endian>* obj =
  584. this->input_object(input_index);
  585. if (obj != NULL)
  586. target->reserve_local_got_entry(i, obj, symndx, got_type & 0x7f);
  587. }
  588. else
  589. {
  590. // This is an entry for a global symbol. GOT_DESC is the symbol
  591. // table index.
  592. // FIXME: This should really be a fatal error (corrupt input).
  593. gold_assert(symndx >= first_global && symndx < symtab_count);
  594. Symbol* sym = this->global_symbol(symndx - first_global);
  595. // Add the GOT entry only if the symbol is still referenced.
  596. if (sym != NULL && sym->in_reg())
  597. {
  598. gold_debug(DEBUG_INCREMENTAL,
  599. "GOT entry %d, type %02x: %s",
  600. i, got_type, sym->name());
  601. target->reserve_global_got_entry(i, sym, got_type);
  602. }
  603. }
  604. }
  605. // Read the PLT entries from the base file and pass each to the target.
  606. for (unsigned int i = 0; i < plt_count; ++i)
  607. {
  608. unsigned int plt_desc = got_plt_reader.get_plt_desc(i);
  609. // FIXME: This should really be a fatal error (corrupt input).
  610. gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
  611. Symbol* sym = this->global_symbol(plt_desc - first_global);
  612. // Add the PLT entry only if the symbol is still referenced.
  613. if (sym != NULL && sym->in_reg())
  614. {
  615. gold_debug(DEBUG_INCREMENTAL,
  616. "PLT entry %d: %s",
  617. i, sym->name());
  618. target->register_global_plt_entry(symtab, layout, i, sym);
  619. }
  620. }
  621. }
  622. // Emit COPY relocations from the existing output file.
  623. template<int size, bool big_endian>
  624. void
  625. Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
  626. Symbol_table* symtab)
  627. {
  628. Sized_target<size, big_endian>* target =
  629. parameters->sized_target<size, big_endian>();
  630. for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
  631. p != this->copy_relocs_.end();
  632. ++p)
  633. {
  634. if (!(*p).symbol->is_copied_from_dynobj())
  635. target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
  636. (*p).offset);
  637. }
  638. }
  639. // Apply incremental relocations for symbols whose values have changed.
  640. template<int size, bool big_endian>
  641. void
  642. Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
  643. const Symbol_table* symtab,
  644. Layout* layout,
  645. Output_file* of)
  646. {
  647. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  648. typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
  649. Incremental_symtab_reader<big_endian> isymtab(this->symtab_reader());
  650. Incremental_relocs_reader<size, big_endian> irelocs(this->relocs_reader());
  651. unsigned int nglobals = isymtab.symbol_count();
  652. const unsigned int incr_reloc_size = irelocs.reloc_size;
  653. Relocate_info<size, big_endian> relinfo;
  654. relinfo.symtab = symtab;
  655. relinfo.layout = layout;
  656. relinfo.object = NULL;
  657. relinfo.reloc_shndx = 0;
  658. relinfo.reloc_shdr = NULL;
  659. relinfo.data_shndx = 0;
  660. relinfo.data_shdr = NULL;
  661. Sized_target<size, big_endian>* target =
  662. parameters->sized_target<size, big_endian>();
  663. for (unsigned int i = 0; i < nglobals; i++)
  664. {
  665. const Symbol* gsym = this->global_symbol(i);
  666. // If the symbol is not referenced from any unchanged input files,
  667. // we do not need to reapply any of its relocations.
  668. if (gsym == NULL)
  669. continue;
  670. // If the symbol is defined in an unchanged file, we do not need to
  671. // reapply any of its relocations.
  672. if (gsym->source() == Symbol::FROM_OBJECT
  673. && gsym->object()->is_incremental())
  674. continue;
  675. gold_debug(DEBUG_INCREMENTAL,
  676. "Applying incremental relocations for global symbol %s [%d]",
  677. gsym->name(), i);
  678. // Follow the linked list of input symbol table entries for this symbol.
  679. // We don't bother to figure out whether the symbol table entry belongs
  680. // to a changed or unchanged file because it's easier just to apply all
  681. // the relocations -- although we might scribble over an area that has
  682. // been reallocated, we do this before copying any new data into the
  683. // output file.
  684. unsigned int offset = isymtab.get_list_head(i);
  685. while (offset > 0)
  686. {
  687. Incremental_global_symbol_reader<big_endian> sym_info =
  688. this->inputs_reader().global_symbol_reader_at_offset(offset);
  689. unsigned int r_base = sym_info.reloc_offset();
  690. unsigned int r_count = sym_info.reloc_count();
  691. // Apply each relocation for this symbol table entry.
  692. for (unsigned int j = 0; j < r_count;
  693. ++j, r_base += incr_reloc_size)
  694. {
  695. unsigned int r_type = irelocs.get_r_type(r_base);
  696. unsigned int r_shndx = irelocs.get_r_shndx(r_base);
  697. Address r_offset = irelocs.get_r_offset(r_base);
  698. Addend r_addend = irelocs.get_r_addend(r_base);
  699. Output_section* os = this->output_section(r_shndx);
  700. Address address = os->address();
  701. off_t section_offset = os->offset();
  702. size_t view_size = os->data_size();
  703. unsigned char* const view = of->get_output_view(section_offset,
  704. view_size);
  705. gold_debug(DEBUG_INCREMENTAL,
  706. " %08lx: %s + %d: type %d addend %ld",
  707. (long)(section_offset + r_offset),
  708. os->name(),
  709. (int)r_offset,
  710. r_type,
  711. (long)r_addend);
  712. target->apply_relocation(&relinfo, r_offset, r_type, r_addend,
  713. gsym, view, address, view_size);
  714. // FIXME: Do something more efficient if write_output_view
  715. // ever becomes more than a no-op.
  716. of->write_output_view(section_offset, view_size, view);
  717. }
  718. offset = sym_info.next_offset();
  719. }
  720. }
  721. }
  722. // Get a view of the main symbol table and the symbol string table.
  723. template<int size, bool big_endian>
  724. void
  725. Sized_incremental_binary<size, big_endian>::get_symtab_view(
  726. View* symtab_view,
  727. unsigned int* nsyms,
  728. elfcpp::Elf_strtab* strtab)
  729. {
  730. *symtab_view = this->view(this->main_symtab_loc_);
  731. *nsyms = this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
  732. View strtab_view(this->view(this->main_strtab_loc_));
  733. *strtab = elfcpp::Elf_strtab(strtab_view.data(),
  734. this->main_strtab_loc_.data_size);
  735. }
  736. namespace
  737. {
  738. // Create a Sized_incremental_binary object of the specified size and
  739. // endianness. Fails if the target architecture is not supported.
  740. template<int size, bool big_endian>
  741. Incremental_binary*
  742. make_sized_incremental_binary(Output_file* file,
  743. const elfcpp::Ehdr<size, big_endian>& ehdr)
  744. {
  745. Target* target = select_target(NULL, 0, // XXX
  746. ehdr.get_e_machine(), size, big_endian,
  747. ehdr.get_e_ident()[elfcpp::EI_OSABI],
  748. ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
  749. if (target == NULL)
  750. {
  751. explain_no_incremental(_("unsupported ELF machine number %d"),
  752. ehdr.get_e_machine());
  753. return NULL;
  754. }
  755. if (!parameters->target_valid())
  756. set_parameters_target(target);
  757. else if (target != &parameters->target())
  758. gold_error(_("%s: incompatible target"), file->filename());
  759. return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
  760. }
  761. } // End of anonymous namespace.
  762. // Create an Incremental_binary object for FILE. Returns NULL is this is not
  763. // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
  764. // should be opened.
  765. Incremental_binary*
  766. open_incremental_binary(Output_file* file)
  767. {
  768. off_t filesize = file->filesize();
  769. int want = elfcpp::Elf_recognizer::max_header_size;
  770. if (filesize < want)
  771. want = filesize;
  772. const unsigned char* p = file->get_input_view(0, want);
  773. if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
  774. {
  775. explain_no_incremental(_("output is not an ELF file."));
  776. return NULL;
  777. }
  778. int size = 0;
  779. bool big_endian = false;
  780. std::string error;
  781. if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
  782. &error))
  783. {
  784. explain_no_incremental(error.c_str());
  785. return NULL;
  786. }
  787. Incremental_binary* result = NULL;
  788. if (size == 32)
  789. {
  790. if (big_endian)
  791. {
  792. #ifdef HAVE_TARGET_32_BIG
  793. result = make_sized_incremental_binary<32, true>(
  794. file, elfcpp::Ehdr<32, true>(p));
  795. #else
  796. explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
  797. #endif
  798. }
  799. else
  800. {
  801. #ifdef HAVE_TARGET_32_LITTLE
  802. result = make_sized_incremental_binary<32, false>(
  803. file, elfcpp::Ehdr<32, false>(p));
  804. #else
  805. explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
  806. #endif
  807. }
  808. }
  809. else if (size == 64)
  810. {
  811. if (big_endian)
  812. {
  813. #ifdef HAVE_TARGET_64_BIG
  814. result = make_sized_incremental_binary<64, true>(
  815. file, elfcpp::Ehdr<64, true>(p));
  816. #else
  817. explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
  818. #endif
  819. }
  820. else
  821. {
  822. #ifdef HAVE_TARGET_64_LITTLE
  823. result = make_sized_incremental_binary<64, false>(
  824. file, elfcpp::Ehdr<64, false>(p));
  825. #else
  826. explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
  827. #endif
  828. }
  829. }
  830. else
  831. gold_unreachable();
  832. return result;
  833. }
  834. // Class Incremental_inputs.
  835. // Add the command line to the string table, setting
  836. // command_line_key_. In incremental builds, the command line is
  837. // stored in .gnu_incremental_inputs so that the next linker run can
  838. // check if the command line options didn't change.
  839. void
  840. Incremental_inputs::report_command_line(int argc, const char* const* argv)
  841. {
  842. // Always store 'gold' as argv[0] to avoid a full relink if the user used a
  843. // different path to the linker.
  844. std::string args("gold");
  845. // Copied from collect_argv in main.cc.
  846. for (int i = 1; i < argc; ++i)
  847. {
  848. // Adding/removing these options should not result in a full relink.
  849. if (strcmp(argv[i], "--incremental") == 0
  850. || strcmp(argv[i], "--incremental-full") == 0
  851. || strcmp(argv[i], "--incremental-update") == 0
  852. || strcmp(argv[i], "--incremental-changed") == 0
  853. || strcmp(argv[i], "--incremental-unchanged") == 0
  854. || strcmp(argv[i], "--incremental-unknown") == 0
  855. || strcmp(argv[i], "--incremental-startup-unchanged") == 0
  856. || is_prefix_of("--incremental-base=", argv[i])
  857. || is_prefix_of("--incremental-patch=", argv[i])
  858. || is_prefix_of("--debug=", argv[i]))
  859. continue;
  860. if (strcmp(argv[i], "--incremental-base") == 0
  861. || strcmp(argv[i], "--incremental-patch") == 0
  862. || strcmp(argv[i], "--debug") == 0)
  863. {
  864. // When these options are used without the '=', skip the
  865. // following parameter as well.
  866. ++i;
  867. continue;
  868. }
  869. args.append(" '");
  870. // Now append argv[i], but with all single-quotes escaped
  871. const char* argpos = argv[i];
  872. while (1)
  873. {
  874. const int len = strcspn(argpos, "'");
  875. args.append(argpos, len);
  876. if (argpos[len] == '\0')
  877. break;
  878. args.append("'\"'\"'");
  879. argpos += len + 1;
  880. }
  881. args.append("'");
  882. }
  883. this->command_line_ = args;
  884. this->strtab_->add(this->command_line_.c_str(), false,
  885. &this->command_line_key_);
  886. }
  887. // Record the input archive file ARCHIVE. This is called by the
  888. // Add_archive_symbols task before determining which archive members
  889. // to include. We create the Incremental_archive_entry here and
  890. // attach it to the Archive, but we do not add it to the list of
  891. // input objects until report_archive_end is called.
  892. void
  893. Incremental_inputs::report_archive_begin(Library_base* arch,
  894. unsigned int arg_serial,
  895. Script_info* script_info)
  896. {
  897. Stringpool::Key filename_key;
  898. Timespec mtime = arch->get_mtime();
  899. // For a file loaded from a script, don't record its argument serial number.
  900. if (script_info != NULL)
  901. arg_serial = 0;
  902. this->strtab_->add(arch->filename().c_str(), false, &filename_key);
  903. Incremental_archive_entry* entry =
  904. new Incremental_archive_entry(filename_key, arg_serial, mtime);
  905. arch->set_incremental_info(entry);
  906. if (script_info != NULL)
  907. {
  908. Incremental_script_entry* script_entry = script_info->incremental_info();
  909. gold_assert(script_entry != NULL);
  910. script_entry->add_object(entry);
  911. }
  912. }
  913. // Visitor class for processing the unused global symbols in a library.
  914. // An instance of this class is passed to the library's
  915. // for_all_unused_symbols() iterator, which will call the visit()
  916. // function for each global symbol defined in each unused library
  917. // member. We add those symbol names to the incremental info for the
  918. // library.
  919. class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
  920. {
  921. public:
  922. Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
  923. : entry_(entry), strtab_(strtab)
  924. { }
  925. void
  926. visit(const char* sym)
  927. {
  928. Stringpool::Key symbol_key;
  929. this->strtab_->add(sym, true, &symbol_key);
  930. this->entry_->add_unused_global_symbol(symbol_key);
  931. }
  932. private:
  933. Incremental_archive_entry* entry_;
  934. Stringpool* strtab_;
  935. };
  936. // Finish recording the input archive file ARCHIVE. This is called by the
  937. // Add_archive_symbols task after determining which archive members
  938. // to include.
  939. void
  940. Incremental_inputs::report_archive_end(Library_base* arch)
  941. {
  942. Incremental_archive_entry* entry = arch->incremental_info();
  943. gold_assert(entry != NULL);
  944. this->inputs_.push_back(entry);
  945. // Collect unused global symbols.
  946. Unused_symbol_visitor v(entry, this->strtab_);
  947. arch->for_all_unused_symbols(&v);
  948. }
  949. // Record the input object file OBJ. If ARCH is not NULL, attach
  950. // the object file to the archive. This is called by the
  951. // Add_symbols task after finding out the type of the file.
  952. void
  953. Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
  954. Library_base* arch, Script_info* script_info)
  955. {
  956. Stringpool::Key filename_key;
  957. Timespec mtime = obj->get_mtime();
  958. // For a file loaded from a script, don't record its argument serial number.
  959. if (script_info != NULL)
  960. arg_serial = 0;
  961. this->strtab_->add(obj->name().c_str(), false, &filename_key);
  962. Incremental_input_entry* input_entry;
  963. this->current_object_ = obj;
  964. if (!obj->is_dynamic())
  965. {
  966. this->current_object_entry_ =
  967. new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
  968. input_entry = this->current_object_entry_;
  969. if (arch != NULL)
  970. {
  971. Incremental_archive_entry* arch_entry = arch->incremental_info();
  972. gold_assert(arch_entry != NULL);
  973. arch_entry->add_object(this->current_object_entry_);
  974. }
  975. }
  976. else
  977. {
  978. this->current_object_entry_ = NULL;
  979. Stringpool::Key soname_key;
  980. Dynobj* dynobj = obj->dynobj();
  981. gold_assert(dynobj != NULL);
  982. this->strtab_->add(dynobj->soname(), false, &soname_key);
  983. input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
  984. arg_serial, mtime);
  985. }
  986. if (obj->is_in_system_directory())
  987. input_entry->set_is_in_system_directory();
  988. if (obj->as_needed())
  989. input_entry->set_as_needed();
  990. this->inputs_.push_back(input_entry);
  991. if (script_info != NULL)
  992. {
  993. Incremental_script_entry* script_entry = script_info->incremental_info();
  994. gold_assert(script_entry != NULL);
  995. script_entry->add_object(input_entry);
  996. }
  997. }
  998. // Record an input section SHNDX from object file OBJ.
  999. void
  1000. Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
  1001. const char* name, off_t sh_size)
  1002. {
  1003. Stringpool::Key key = 0;
  1004. if (name != NULL)
  1005. this->strtab_->add(name, true, &key);
  1006. gold_assert(obj == this->current_object_);
  1007. gold_assert(this->current_object_entry_ != NULL);
  1008. this->current_object_entry_->add_input_section(shndx, key, sh_size);
  1009. }
  1010. // Record a kept COMDAT group belonging to object file OBJ.
  1011. void
  1012. Incremental_inputs::report_comdat_group(Object* obj, const char* name)
  1013. {
  1014. Stringpool::Key key = 0;
  1015. if (name != NULL)
  1016. this->strtab_->add(name, true, &key);
  1017. gold_assert(obj == this->current_object_);
  1018. gold_assert(this->current_object_entry_ != NULL);
  1019. this->current_object_entry_->add_comdat_group(key);
  1020. }
  1021. // Record that the input argument INPUT is a script SCRIPT. This is
  1022. // called by read_script after parsing the script and reading the list
  1023. // of inputs added by this script.
  1024. void
  1025. Incremental_inputs::report_script(Script_info* script,
  1026. unsigned int arg_serial,
  1027. Timespec mtime)
  1028. {
  1029. Stringpool::Key filename_key;
  1030. this->strtab_->add(script->filename().c_str(), false, &filename_key);
  1031. Incremental_script_entry* entry =
  1032. new Incremental_script_entry(filename_key, arg_serial, script, mtime);
  1033. this->inputs_.push_back(entry);
  1034. script->set_incremental_info(entry);
  1035. }
  1036. // Finalize the incremental link information. Called from
  1037. // Layout::finalize.
  1038. void
  1039. Incremental_inputs::finalize()
  1040. {
  1041. // Finalize the string table.
  1042. this->strtab_->set_string_offsets();
  1043. }
  1044. // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
  1045. void
  1046. Incremental_inputs::create_data_sections(Symbol_table* symtab)
  1047. {
  1048. int reloc_align = 4;
  1049. switch (parameters->size_and_endianness())
  1050. {
  1051. #ifdef HAVE_TARGET_32_LITTLE
  1052. case Parameters::TARGET_32_LITTLE:
  1053. this->inputs_section_ =
  1054. new Output_section_incremental_inputs<32, false>(this, symtab);
  1055. reloc_align = 4;
  1056. break;
  1057. #endif
  1058. #ifdef HAVE_TARGET_32_BIG
  1059. case Parameters::TARGET_32_BIG:
  1060. this->inputs_section_ =
  1061. new Output_section_incremental_inputs<32, true>(this, symtab);
  1062. reloc_align = 4;
  1063. break;
  1064. #endif
  1065. #ifdef HAVE_TARGET_64_LITTLE
  1066. case Parameters::TARGET_64_LITTLE:
  1067. this->inputs_section_ =
  1068. new Output_section_incremental_inputs<64, false>(this, symtab);
  1069. reloc_align = 8;
  1070. break;
  1071. #endif
  1072. #ifdef HAVE_TARGET_64_BIG
  1073. case Parameters::TARGET_64_BIG:
  1074. this->inputs_section_ =
  1075. new Output_section_incremental_inputs<64, true>(this, symtab);
  1076. reloc_align = 8;
  1077. break;
  1078. #endif
  1079. default:
  1080. gold_unreachable();
  1081. }
  1082. this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
  1083. this->relocs_section_ = new Output_data_space(reloc_align,
  1084. "** incremental_relocs");
  1085. this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
  1086. }
  1087. // Return the sh_entsize value for the .gnu_incremental_relocs section.
  1088. unsigned int
  1089. Incremental_inputs::relocs_entsize() const
  1090. {
  1091. return 8 + 2 * parameters->target().get_size() / 8;
  1092. }
  1093. // Class Output_section_incremental_inputs.
  1094. // Finalize the offsets for each input section and supplemental info block,
  1095. // and set the final data size of the incremental output sections.
  1096. template<int size, bool big_endian>
  1097. void
  1098. Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
  1099. {
  1100. const Incremental_inputs* inputs = this->inputs_;
  1101. // Offset of each input entry.
  1102. unsigned int input_offset = this->header_size;
  1103. // Offset of each supplemental info block.
  1104. unsigned int file_index = 0;
  1105. unsigned int info_offset = this->header_size;
  1106. info_offset += this->input_entry_size * inputs->input_file_count();
  1107. // Count each input file and its supplemental information block.
  1108. for (Incremental_inputs::Input_list::const_iterator p =
  1109. inputs->input_files().begin();
  1110. p != inputs->input_files().end();
  1111. ++p)
  1112. {
  1113. // Set the index and offset of the input file entry.
  1114. (*p)->set_offset(file_index, input_offset);
  1115. ++file_index;
  1116. input_offset += this->input_entry_size;
  1117. // Set the offset of the supplemental info block.
  1118. switch ((*p)->type())
  1119. {
  1120. case INCREMENTAL_INPUT_SCRIPT:
  1121. {
  1122. Incremental_script_entry *entry = (*p)->script_entry();
  1123. gold_assert(entry != NULL);
  1124. (*p)->set_info_offset(info_offset);
  1125. // Object count.
  1126. info_offset += 4;
  1127. // Each member.
  1128. info_offset += (entry->get_object_count() * 4);
  1129. }
  1130. break;
  1131. case INCREMENTAL_INPUT_OBJECT:
  1132. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  1133. {
  1134. Incremental_object_entry* entry = (*p)->object_entry();
  1135. gold_assert(entry != NULL);
  1136. (*p)->set_info_offset(info_offset);
  1137. // Input section count, global symbol count, local symbol offset,
  1138. // local symbol count, first dynamic reloc, dynamic reloc count,
  1139. // comdat group count.
  1140. info_offset += this->object_info_size;
  1141. // Each input section.
  1142. info_offset += (entry->get_input_section_count()
  1143. * this->input_section_entry_size);
  1144. // Each global symbol.
  1145. const Object::Symbols* syms = entry->object()->get_global_symbols();
  1146. info_offset += syms->size() * this->global_sym_entry_size;
  1147. // Each comdat group.
  1148. info_offset += entry->get_comdat_group_count() * 4;
  1149. }
  1150. break;
  1151. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  1152. {
  1153. Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
  1154. gold_assert(entry != NULL);
  1155. (*p)->set_info_offset(info_offset);
  1156. // Global symbol count, soname index.
  1157. info_offset += 8;
  1158. // Each global symbol.
  1159. const Object::Symbols* syms = entry->object()->get_global_symbols();
  1160. gold_assert(syms != NULL);
  1161. unsigned int nsyms = syms->size();
  1162. unsigned int nsyms_out = 0;
  1163. for (unsigned int i = 0; i < nsyms; ++i)
  1164. {
  1165. const Symbol* sym = (*syms)[i];
  1166. if (sym == NULL)
  1167. continue;
  1168. if (sym->is_forwarder())
  1169. sym = this->symtab_->resolve_forwards(sym);
  1170. if (sym->symtab_index() != -1U)
  1171. ++nsyms_out;
  1172. }
  1173. info_offset += nsyms_out * 4;
  1174. }
  1175. break;
  1176. case INCREMENTAL_INPUT_ARCHIVE:
  1177. {
  1178. Incremental_archive_entry* entry = (*p)->archive_entry();
  1179. gold_assert(entry != NULL);
  1180. (*p)->set_info_offset(info_offset);
  1181. // Member count + unused global symbol count.
  1182. info_offset += 8;
  1183. // Each member.
  1184. info_offset += (entry->get_member_count() * 4);
  1185. // Each global symbol.
  1186. info_offset += (entry->get_unused_global_symbol_count() * 4);
  1187. }
  1188. break;
  1189. default:
  1190. gold_unreachable();
  1191. }
  1192. // Pad so each supplemental info block begins at an 8-byte boundary.
  1193. if (info_offset & 4)
  1194. info_offset += 4;
  1195. }
  1196. this->set_data_size(info_offset);
  1197. // Set the size of the .gnu_incremental_symtab section.
  1198. inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
  1199. * sizeof(unsigned int));
  1200. // Set the size of the .gnu_incremental_relocs section.
  1201. inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
  1202. * this->incr_reloc_size);
  1203. // Set the size of the .gnu_incremental_got_plt section.
  1204. Sized_target<size, big_endian>* target =
  1205. parameters->sized_target<size, big_endian>();
  1206. unsigned int got_count = target->got_entry_count();
  1207. unsigned int plt_count = target->plt_entry_count();
  1208. unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
  1209. got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
  1210. got_plt_size += got_count * 8 + plt_count * 4; // GOT array, PLT array.
  1211. inputs->got_plt_section()->set_current_data_size(got_plt_size);
  1212. }
  1213. // Write the contents of the .gnu_incremental_inputs and
  1214. // .gnu_incremental_symtab sections.
  1215. template<int size, bool big_endian>
  1216. void
  1217. Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
  1218. {
  1219. const Incremental_inputs* inputs = this->inputs_;
  1220. Stringpool* strtab = inputs->get_stringpool();
  1221. // Get a view into the .gnu_incremental_inputs section.
  1222. const off_t off = this->offset();
  1223. const off_t oview_size = this->data_size();
  1224. unsigned char* const oview = of->get_output_view(off, oview_size);
  1225. unsigned char* pov = oview;
  1226. // Get a view into the .gnu_incremental_symtab section.
  1227. const off_t symtab_off = inputs->symtab_section()->offset();
  1228. const off_t symtab_size = inputs->symtab_section()->data_size();
  1229. unsigned char* const symtab_view = of->get_output_view(symtab_off,
  1230. symtab_size);
  1231. // Allocate an array of linked list heads for the .gnu_incremental_symtab
  1232. // section. Each element corresponds to a global symbol in the output
  1233. // symbol table, and points to the head of the linked list that threads
  1234. // through the object file input entries. The value of each element
  1235. // is the section-relative offset to a global symbol entry in a
  1236. // supplemental information block.
  1237. unsigned int global_sym_count = this->symtab_->output_count();
  1238. unsigned int* global_syms = new unsigned int[global_sym_count];
  1239. memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
  1240. // Write the section header.
  1241. Stringpool::Key command_line_key = inputs->command_line_key();
  1242. pov = this->write_header(pov, inputs->input_file_count(),
  1243. strtab->get_offset_from_key(command_line_key));
  1244. // Write the list of input files.
  1245. pov = this->write_input_files(oview, pov, strtab);
  1246. // Write the supplemental information blocks for each input file.
  1247. pov = this->write_info_blocks(oview, pov, strtab, global_syms,
  1248. global_sym_count);
  1249. gold_assert(pov - oview == oview_size);
  1250. // Write the .gnu_incremental_symtab section.
  1251. gold_assert(static_cast<off_t>(global_sym_count) * 4 == symtab_size);
  1252. this->write_symtab(symtab_view, global_syms, global_sym_count);
  1253. delete[] global_syms;
  1254. // Write the .gnu_incremental_got_plt section.
  1255. const off_t got_plt_off = inputs->got_plt_section()->offset();
  1256. const off_t got_plt_size = inputs->got_plt_section()->data_size();
  1257. unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
  1258. got_plt_size);
  1259. this->write_got_plt(got_plt_view, got_plt_size);
  1260. of->write_output_view(off, oview_size, oview);
  1261. of->write_output_view(symtab_off, symtab_size, symtab_view);
  1262. of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
  1263. }
  1264. // Write the section header: version, input file count, offset of command line
  1265. // in the string table, and 4 bytes of padding.
  1266. template<int size, bool big_endian>
  1267. unsigned char*
  1268. Output_section_incremental_inputs<size, big_endian>::write_header(
  1269. unsigned char* pov,
  1270. unsigned int input_file_count,
  1271. section_offset_type command_line_offset)
  1272. {
  1273. Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
  1274. Swap32::writeval(pov + 4, input_file_count);
  1275. Swap32::writeval(pov + 8, command_line_offset);
  1276. Swap32::writeval(pov + 12, 0);
  1277. gold_assert(this->header_size == 16);
  1278. return pov + this->header_size;
  1279. }
  1280. // Write the input file entries.
  1281. template<int size, bool big_endian>
  1282. unsigned char*
  1283. Output_section_incremental_inputs<size, big_endian>::write_input_files(
  1284. unsigned char* oview,
  1285. unsigned char* pov,
  1286. Stringpool* strtab)
  1287. {
  1288. const Incremental_inputs* inputs = this->inputs_;
  1289. for (Incremental_inputs::Input_list::const_iterator p =
  1290. inputs->input_files().begin();
  1291. p != inputs->input_files().end();
  1292. ++p)
  1293. {
  1294. gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
  1295. section_offset_type filename_offset =
  1296. strtab->get_offset_from_key((*p)->get_filename_key());
  1297. const Timespec& mtime = (*p)->get_mtime();
  1298. unsigned int flags = (*p)->type();
  1299. if ((*p)->is_in_system_directory())
  1300. flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
  1301. if ((*p)->as_needed())
  1302. flags |= INCREMENTAL_INPUT_AS_NEEDED;
  1303. Swap32::writeval(pov, filename_offset);
  1304. Swap32::writeval(pov + 4, (*p)->get_info_offset());
  1305. Swap64::writeval(pov + 8, mtime.seconds);
  1306. Swap32::writeval(pov + 16, mtime.nanoseconds);
  1307. Swap16::writeval(pov + 20, flags);
  1308. Swap16::writeval(pov + 22, (*p)->arg_serial());
  1309. gold_assert(this->input_entry_size == 24);
  1310. pov += this->input_entry_size;
  1311. }
  1312. return pov;
  1313. }
  1314. // Write the supplemental information blocks.
  1315. template<int size, bool big_endian>
  1316. unsigned char*
  1317. Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
  1318. unsigned char* oview,
  1319. unsigned char* pov,
  1320. Stringpool* strtab,
  1321. unsigned int* global_syms,
  1322. unsigned int global_sym_count)
  1323. {
  1324. const Incremental_inputs* inputs = this->inputs_;
  1325. unsigned int first_global_index = this->symtab_->first_global_index();
  1326. for (Incremental_inputs::Input_list::const_iterator p =
  1327. inputs->input_files().begin();
  1328. p != inputs->input_files().end();
  1329. ++p)
  1330. {
  1331. switch ((*p)->type())
  1332. {
  1333. case INCREMENTAL_INPUT_SCRIPT:
  1334. {
  1335. gold_assert(static_cast<unsigned int>(pov - oview)
  1336. == (*p)->get_info_offset());
  1337. Incremental_script_entry* entry = (*p)->script_entry();
  1338. gold_assert(entry != NULL);
  1339. // Write the object count.
  1340. unsigned int nobjects = entry->get_object_count();
  1341. Swap32::writeval(pov, nobjects);
  1342. pov += 4;
  1343. // For each object, write the offset to its input file entry.
  1344. for (unsigned int i = 0; i < nobjects; ++i)
  1345. {
  1346. Incremental_input_entry* obj = entry->get_object(i);
  1347. Swap32::writeval(pov, obj->get_offset());
  1348. pov += 4;
  1349. }
  1350. }
  1351. break;
  1352. case INCREMENTAL_INPUT_OBJECT:
  1353. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  1354. {
  1355. gold_assert(static_cast<unsigned int>(pov - oview)
  1356. == (*p)->get_info_offset());
  1357. Incremental_object_entry* entry = (*p)->object_entry();
  1358. gold_assert(entry != NULL);
  1359. const Object* obj = entry->object();
  1360. const Relobj* relobj = static_cast<const Relobj*>(obj);
  1361. const Object::Symbols* syms = obj->get_global_symbols();
  1362. // Write the input section count and global symbol count.
  1363. unsigned int nsections = entry->get_input_section_count();
  1364. unsigned int nsyms = syms->size();
  1365. off_t locals_offset = relobj->local_symbol_offset();
  1366. unsigned int nlocals = relobj->output_local_symbol_count();
  1367. unsigned int first_dynrel = relobj->first_dyn_reloc();
  1368. unsigned int ndynrel = relobj->dyn_reloc_count();
  1369. unsigned int ncomdat = entry->get_comdat_group_count();
  1370. Swap32::writeval(pov, nsections);
  1371. Swap32::writeval(pov + 4, nsyms);
  1372. Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
  1373. Swap32::writeval(pov + 12, nlocals);
  1374. Swap32::writeval(pov + 16, first_dynrel);
  1375. Swap32::writeval(pov + 20, ndynrel);
  1376. Swap32::writeval(pov + 24, ncomdat);
  1377. Swap32::writeval(pov + 28, 0);
  1378. gold_assert(this->object_info_size == 32);
  1379. pov += this->object_info_size;
  1380. // Build a temporary array to map input section indexes
  1381. // from the original object file index to the index in the
  1382. // incremental info table.
  1383. unsigned int* index_map = new unsigned int[obj->shnum()];
  1384. memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
  1385. // For each input section, write the name, output section index,
  1386. // offset within output section, and input section size.
  1387. for (unsigned int i = 0; i < nsections; i++)
  1388. {
  1389. unsigned int shndx = entry->get_input_section_index(i);
  1390. index_map[shndx] = i + 1;
  1391. Stringpool::Key key = entry->get_input_section_name_key(i);
  1392. off_t name_offset = 0;
  1393. if (key != 0)
  1394. name_offset = strtab->get_offset_from_key(key);
  1395. int out_shndx = 0;
  1396. off_t out_offset = 0;
  1397. off_t sh_size = 0;
  1398. Output_section* os = obj->output_section(shndx);
  1399. if (os != NULL)
  1400. {
  1401. out_shndx = os->out_shndx();
  1402. out_offset = obj->output_section_offset(shndx);
  1403. sh_size = entry->get_input_section_size(i);
  1404. }
  1405. Swap32::writeval(pov, name_offset);
  1406. Swap32::writeval(pov + 4, out_shndx);
  1407. Swap::writeval(pov + 8, out_offset);
  1408. Swap::writeval(pov + 8 + sizeof_addr, sh_size);
  1409. gold_assert(this->input_section_entry_size
  1410. == 8 + 2 * sizeof_addr);
  1411. pov += this->input_section_entry_size;
  1412. }
  1413. // For each global symbol, write its associated relocations,
  1414. // add it to the linked list of globals, then write the
  1415. // supplemental information: global symbol table index,
  1416. // input section index, linked list chain pointer, relocation
  1417. // count, and offset to the relocations.
  1418. for (unsigned int i = 0; i < nsyms; i++)
  1419. {
  1420. const Symbol* sym = (*syms)[i];
  1421. if (sym->is_forwarder())
  1422. sym = this->symtab_->resolve_forwards(sym);
  1423. unsigned int shndx = 0;
  1424. if (sym->source() != Symbol::FROM_OBJECT)
  1425. {
  1426. // The symbol was defined by the linker (e.g., common).
  1427. // We mark these symbols with a special SHNDX of -1,
  1428. // but exclude linker-predefined symbols and symbols
  1429. // copied from shared objects.
  1430. if (!sym->is_predefined()
  1431. && !sym->is_copied_from_dynobj())
  1432. shndx = -1U;
  1433. }
  1434. else if (sym->object() == obj && sym->is_defined())
  1435. {
  1436. bool is_ordinary;
  1437. unsigned int orig_shndx = sym->shndx(&is_ordinary);
  1438. if (is_ordinary)
  1439. shndx = index_map[orig_shndx];
  1440. else
  1441. shndx = 1;
  1442. }
  1443. unsigned int symtab_index = sym->symtab_index();
  1444. unsigned int chain = 0;
  1445. unsigned int first_reloc = 0;
  1446. unsigned int nrelocs = obj->get_incremental_reloc_count(i);
  1447. if (nrelocs > 0)
  1448. {
  1449. gold_assert(symtab_index != -1U
  1450. && (symtab_index - first_global_index
  1451. < global_sym_count));
  1452. first_reloc = obj->get_incremental_reloc_base(i);
  1453. chain = global_syms[symtab_index - first_global_index];
  1454. global_syms[symtab_index - first_global_index] =
  1455. pov - oview;
  1456. }
  1457. Swap32::writeval(pov, symtab_index);
  1458. Swap32::writeval(pov + 4, shndx);
  1459. Swap32::writeval(pov + 8, chain);
  1460. Swap32::writeval(pov + 12, nrelocs);
  1461. Swap32::writeval(pov + 16,
  1462. first_reloc * (8 + 2 * sizeof_addr));
  1463. gold_assert(this->global_sym_entry_size == 20);
  1464. pov += this->global_sym_entry_size;
  1465. }
  1466. // For each kept COMDAT group, write the group signature.
  1467. for (unsigned int i = 0; i < ncomdat; i++)
  1468. {
  1469. Stringpool::Key key = entry->get_comdat_signature_key(i);
  1470. off_t name_offset = 0;
  1471. if (key != 0)
  1472. name_offset = strtab->get_offset_from_key(key);
  1473. Swap32::writeval(pov, name_offset);
  1474. pov += 4;
  1475. }
  1476. delete[] index_map;
  1477. }
  1478. break;
  1479. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  1480. {
  1481. gold_assert(static_cast<unsigned int>(pov - oview)
  1482. == (*p)->get_info_offset());
  1483. Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
  1484. gold_assert(entry != NULL);
  1485. Object* obj = entry->object();
  1486. Dynobj* dynobj = obj->dynobj();
  1487. gold_assert(dynobj != NULL);
  1488. const Object::Symbols* syms = obj->get_global_symbols();
  1489. // Write the soname string table index.
  1490. section_offset_type soname_offset =
  1491. strtab->get_offset_from_key(entry->get_soname_key());
  1492. Swap32::writeval(pov, soname_offset);
  1493. pov += 4;
  1494. // Skip the global symbol count for now.
  1495. unsigned char* orig_pov = pov;
  1496. pov += 4;
  1497. // For each global symbol, write the global symbol table index.
  1498. unsigned int nsyms = syms->size();
  1499. unsigned int nsyms_out = 0;
  1500. for (unsigned int i = 0; i < nsyms; i++)
  1501. {
  1502. const Symbol* sym = (*syms)[i];
  1503. if (sym == NULL)
  1504. continue;
  1505. if (sym->is_forwarder())
  1506. sym = this->symtab_->resolve_forwards(sym);
  1507. if (sym->symtab_index() == -1U)
  1508. continue;
  1509. unsigned int flags = 0;
  1510. // If the symbol has hidden or internal visibility, we
  1511. // mark it as defined in the shared object so we don't
  1512. // try to resolve it during an incremental update.
  1513. if (sym->visibility() == elfcpp::STV_HIDDEN
  1514. || sym->visibility() == elfcpp::STV_INTERNAL)
  1515. flags = INCREMENTAL_SHLIB_SYM_DEF;
  1516. else if (sym->source() == Symbol::FROM_OBJECT
  1517. && sym->object() == obj
  1518. && sym->is_defined())
  1519. flags = INCREMENTAL_SHLIB_SYM_DEF;
  1520. else if (sym->is_copied_from_dynobj()
  1521. && this->symtab_->get_copy_source(sym) == dynobj)
  1522. flags = INCREMENTAL_SHLIB_SYM_COPY;
  1523. flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
  1524. Swap32::writeval(pov, sym->symtab_index() | flags);
  1525. pov += 4;
  1526. ++nsyms_out;
  1527. }
  1528. // Now write the global symbol count.
  1529. Swap32::writeval(orig_pov, nsyms_out);
  1530. }
  1531. break;
  1532. case INCREMENTAL_INPUT_ARCHIVE:
  1533. {
  1534. gold_assert(static_cast<unsigned int>(pov - oview)
  1535. == (*p)->get_info_offset());
  1536. Incremental_archive_entry* entry = (*p)->archive_entry();
  1537. gold_assert(entry != NULL);
  1538. // Write the member count and unused global symbol count.
  1539. unsigned int nmembers = entry->get_member_count();
  1540. unsigned int nsyms = entry->get_unused_global_symbol_count();
  1541. Swap32::writeval(pov, nmembers);
  1542. Swap32::writeval(pov + 4, nsyms);
  1543. pov += 8;
  1544. // For each member, write the offset to its input file entry.
  1545. for (unsigned int i = 0; i < nmembers; ++i)
  1546. {
  1547. Incremental_object_entry* member = entry->get_member(i);
  1548. Swap32::writeval(pov, member->get_offset());
  1549. pov += 4;
  1550. }
  1551. // For each global symbol, write the name offset.
  1552. for (unsigned int i = 0; i < nsyms; ++i)
  1553. {
  1554. Stringpool::Key key = entry->get_unused_global_symbol(i);
  1555. Swap32::writeval(pov, strtab->get_offset_from_key(key));
  1556. pov += 4;
  1557. }
  1558. }
  1559. break;
  1560. default:
  1561. gold_unreachable();
  1562. }
  1563. // Pad the info block to a multiple of 8 bytes.
  1564. if (static_cast<unsigned int>(pov - oview) & 4)
  1565. {
  1566. Swap32::writeval(pov, 0);
  1567. pov += 4;
  1568. }
  1569. }
  1570. return pov;
  1571. }
  1572. // Write the contents of the .gnu_incremental_symtab section.
  1573. template<int size, bool big_endian>
  1574. void
  1575. Output_section_incremental_inputs<size, big_endian>::write_symtab(
  1576. unsigned char* pov,
  1577. unsigned int* global_syms,
  1578. unsigned int global_sym_count)
  1579. {
  1580. for (unsigned int i = 0; i < global_sym_count; ++i)
  1581. {
  1582. Swap32::writeval(pov, global_syms[i]);
  1583. pov += 4;
  1584. }
  1585. }
  1586. // This struct holds the view information needed to write the
  1587. // .gnu_incremental_got_plt section.
  1588. struct Got_plt_view_info
  1589. {
  1590. // Start of the GOT type array in the output view.
  1591. unsigned char* got_type_p;
  1592. // Start of the GOT descriptor array in the output view.
  1593. unsigned char* got_desc_p;
  1594. // Start of the PLT descriptor array in the output view.
  1595. unsigned char* plt_desc_p;
  1596. // Number of GOT entries.
  1597. unsigned int got_count;
  1598. // Number of PLT entries.
  1599. unsigned int plt_count;
  1600. // Offset of the first non-reserved PLT entry (this is a target-dependent value).
  1601. unsigned int first_plt_entry_offset;
  1602. // Size of a PLT entry (this is a target-dependent value).
  1603. unsigned int plt_entry_size;
  1604. // Symbol index to write in the GOT descriptor array. For global symbols,
  1605. // this is the global symbol table index; for local symbols, it is the
  1606. // local symbol table index.
  1607. unsigned int sym_index;
  1608. // Input file index to write in the GOT descriptor array. For global
  1609. // symbols, this is 0; for local symbols, it is the index of the input
  1610. // file entry in the .gnu_incremental_inputs section.
  1611. unsigned int input_index;
  1612. };
  1613. // Functor class for processing a GOT offset list for local symbols.
  1614. // Writes the GOT type and symbol index into the GOT type and descriptor
  1615. // arrays in the output section.
  1616. template<int size, bool big_endian>
  1617. class Local_got_offset_visitor : public Got_offset_list::Visitor
  1618. {
  1619. public:
  1620. Local_got_offset_visitor(struct Got_plt_view_info& info)
  1621. : info_(info)
  1622. { }
  1623. void
  1624. visit(unsigned int got_type, unsigned int got_offset)
  1625. {
  1626. unsigned int got_index = got_offset / this->got_entry_size_;
  1627. gold_assert(got_index < this->info_.got_count);
  1628. // We can only handle GOT entry types in the range 0..0x7e
  1629. // because we use a byte array to store them, and we use the
  1630. // high bit to flag a local symbol.
  1631. gold_assert(got_type < 0x7f);
  1632. this->info_.got_type_p[got_index] = got_type | 0x80;
  1633. unsigned char* pov = this->info_.got_desc_p + got_index * 8;
  1634. elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
  1635. elfcpp::Swap<32, big_endian>::writeval(pov + 4, this->info_.input_index);
  1636. }
  1637. private:
  1638. static const unsigned int got_entry_size_ = size / 8;
  1639. struct Got_plt_view_info& info_;
  1640. };
  1641. // Functor class for processing a GOT offset list. Writes the GOT type
  1642. // and symbol index into the GOT type and descriptor arrays in the output
  1643. // section.
  1644. template<int size, bool big_endian>
  1645. class Global_got_offset_visitor : public Got_offset_list::Visitor
  1646. {
  1647. public:
  1648. Global_got_offset_visitor(struct Got_plt_view_info& info)
  1649. : info_(info)
  1650. { }
  1651. void
  1652. visit(unsigned int got_type, unsigned int got_offset)
  1653. {
  1654. unsigned int got_index = got_offset / this->got_entry_size_;
  1655. gold_assert(got_index < this->info_.got_count);
  1656. // We can only handle GOT entry types in the range 0..0x7e
  1657. // because we use a byte array to store them, and we use the
  1658. // high bit to flag a local symbol.
  1659. gold_assert(got_type < 0x7f);
  1660. this->info_.got_type_p[got_index] = got_type;
  1661. unsigned char* pov = this->info_.got_desc_p + got_index * 8;
  1662. elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
  1663. elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
  1664. }
  1665. private:
  1666. static const unsigned int got_entry_size_ = size / 8;
  1667. struct Got_plt_view_info& info_;
  1668. };
  1669. // Functor class for processing the global symbol table. Processes the
  1670. // GOT offset list for the symbol, and writes the symbol table index
  1671. // into the PLT descriptor array in the output section.
  1672. template<int size, bool big_endian>
  1673. class Global_symbol_visitor_got_plt
  1674. {
  1675. public:
  1676. Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
  1677. : info_(info)
  1678. { }
  1679. void
  1680. operator()(const Sized_symbol<size>* sym)
  1681. {
  1682. typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
  1683. const Got_offset_list* got_offsets = sym->got_offset_list();
  1684. if (got_offsets != NULL)
  1685. {
  1686. this->info_.sym_index = sym->symtab_index();
  1687. this->info_.input_index = 0;
  1688. Got_visitor v(this->info_);
  1689. got_offsets->for_all_got_offsets(&v);
  1690. }
  1691. if (sym->has_plt_offset())
  1692. {
  1693. unsigned int plt_index =
  1694. ((sym->plt_offset() - this->info_.first_plt_entry_offset)
  1695. / this->info_.plt_entry_size);
  1696. gold_assert(plt_index < this->info_.plt_count);
  1697. unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
  1698. elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
  1699. }
  1700. }
  1701. private:
  1702. struct Got_plt_view_info& info_;
  1703. };
  1704. // Write the contents of the .gnu_incremental_got_plt section.
  1705. template<int size, bool big_endian>
  1706. void
  1707. Output_section_incremental_inputs<size, big_endian>::write_got_plt(
  1708. unsigned char* pov,
  1709. off_t view_size)
  1710. {
  1711. Sized_target<size, big_endian>* target =
  1712. parameters->sized_target<size, big_endian>();
  1713. // Set up the view information for the functors.
  1714. struct Got_plt_view_info view_info;
  1715. view_info.got_count = target->got_entry_count();
  1716. view_info.plt_count = target->plt_entry_count();
  1717. view_info.first_plt_entry_offset = target->first_plt_entry_offset();
  1718. view_info.plt_entry_size = target->plt_entry_size();
  1719. view_info.got_type_p = pov + 8;
  1720. view_info.got_desc_p = (view_info.got_type_p
  1721. + ((view_info.got_count + 3) & ~3));
  1722. view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
  1723. gold_assert(pov + view_size ==
  1724. view_info.plt_desc_p + view_info.plt_count * 4);
  1725. // Write the section header.
  1726. Swap32::writeval(pov, view_info.got_count);
  1727. Swap32::writeval(pov + 4, view_info.plt_count);
  1728. // Initialize the GOT type array to 0xff (reserved).
  1729. memset(view_info.got_type_p, 0xff, view_info.got_count);
  1730. // Write the incremental GOT descriptors for local symbols.
  1731. typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
  1732. for (Incremental_inputs::Input_list::const_iterator p =
  1733. this->inputs_->input_files().begin();
  1734. p != this->inputs_->input_files().end();
  1735. ++p)
  1736. {
  1737. if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
  1738. && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
  1739. continue;
  1740. Incremental_object_entry* entry = (*p)->object_entry();
  1741. gold_assert(entry != NULL);
  1742. const Object* obj = entry->object();
  1743. gold_assert(obj != NULL);
  1744. view_info.input_index = (*p)->get_file_index();
  1745. Got_visitor v(view_info);
  1746. obj->for_all_local_got_entries(&v);
  1747. }
  1748. // Write the incremental GOT and PLT descriptors for global symbols.
  1749. typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
  1750. symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
  1751. }
  1752. // Class Sized_relobj_incr. Most of these methods are not used for
  1753. // Incremental objects, but are required to be implemented by the
  1754. // base class Object.
  1755. template<int size, bool big_endian>
  1756. Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
  1757. const std::string& name,
  1758. Sized_incremental_binary<size, big_endian>* ibase,
  1759. unsigned int input_file_index)
  1760. : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
  1761. input_file_index_(input_file_index),
  1762. input_reader_(ibase->inputs_reader().input_file(input_file_index)),
  1763. local_symbol_count_(0), output_local_dynsym_count_(0),
  1764. local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
  1765. symbols_(), defined_count_(0), incr_reloc_offset_(-1U),
  1766. incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL),
  1767. local_symbols_()
  1768. {
  1769. if (this->input_reader_.is_in_system_directory())
  1770. this->set_is_in_system_directory();
  1771. const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
  1772. this->set_shnum(shnum);
  1773. ibase->set_input_object(input_file_index, this);
  1774. }
  1775. // Read the symbols.
  1776. template<int size, bool big_endian>
  1777. void
  1778. Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
  1779. {
  1780. gold_unreachable();
  1781. }
  1782. // Lay out the input sections.
  1783. template<int size, bool big_endian>
  1784. void
  1785. Sized_relobj_incr<size, big_endian>::do_layout(
  1786. Symbol_table*,
  1787. Layout* layout,
  1788. Read_symbols_data*)
  1789. {
  1790. const unsigned int shnum = this->shnum();
  1791. Incremental_inputs* incremental_inputs = layout->incremental_inputs();
  1792. gold_assert(incremental_inputs != NULL);
  1793. Output_sections& out_sections(this->output_sections());
  1794. out_sections.resize(shnum);
  1795. this->section_offsets().resize(shnum);
  1796. // Keep track of .debug_info and .debug_types sections.
  1797. std::vector<unsigned int> debug_info_sections;
  1798. std::vector<unsigned int> debug_types_sections;
  1799. for (unsigned int i = 1; i < shnum; i++)
  1800. {
  1801. typename Input_entry_reader::Input_section_info sect =
  1802. this->input_reader_.get_input_section(i - 1);
  1803. // Add the section to the incremental inputs layout.
  1804. incremental_inputs->report_input_section(this, i, sect.name,
  1805. sect.sh_size);
  1806. if (sect.output_shndx == 0 || sect.sh_offset == -1)
  1807. continue;
  1808. Output_section* os = this->ibase_->output_section(sect.output_shndx);
  1809. gold_assert(os != NULL);
  1810. out_sections[i] = os;
  1811. this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
  1812. // When generating a .gdb_index section, we do additional
  1813. // processing of .debug_info and .debug_types sections after all
  1814. // the other sections.
  1815. if (parameters->options().gdb_index())
  1816. {
  1817. const char* name = os->name();
  1818. if (strcmp(name, ".debug_info") == 0)
  1819. debug_info_sections.push_back(i);
  1820. else if (strcmp(name, ".debug_types") == 0)
  1821. debug_types_sections.push_back(i);
  1822. }
  1823. }
  1824. // Process the COMDAT groups.
  1825. unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
  1826. for (unsigned int i = 0; i < ncomdat; i++)
  1827. {
  1828. const char* signature = this->input_reader_.get_comdat_group_signature(i);
  1829. if (signature == NULL || signature[0] == '\0')
  1830. this->error(_("COMDAT group has no signature"));
  1831. bool keep = layout->find_or_add_kept_section(signature, this, i, true,
  1832. true, NULL);
  1833. if (keep)
  1834. incremental_inputs->report_comdat_group(this, signature);
  1835. else
  1836. this->error(_("COMDAT group %s included twice in incremental link"),
  1837. signature);
  1838. }
  1839. // When building a .gdb_index section, scan the .debug_info and
  1840. // .debug_types sections.
  1841. for (std::vector<unsigned int>::const_iterator p
  1842. = debug_info_sections.begin();
  1843. p != debug_info_sections.end();
  1844. ++p)
  1845. {
  1846. unsigned int i = *p;
  1847. layout->add_to_gdb_index(false, this, NULL, 0, i, 0, 0);
  1848. }
  1849. for (std::vector<unsigned int>::const_iterator p
  1850. = debug_types_sections.begin();
  1851. p != debug_types_sections.end();
  1852. ++p)
  1853. {
  1854. unsigned int i = *p;
  1855. layout->add_to_gdb_index(true, this, 0, 0, i, 0, 0);
  1856. }
  1857. }
  1858. // Layout sections whose layout was deferred while waiting for
  1859. // input files from a plugin.
  1860. template<int size, bool big_endian>
  1861. void
  1862. Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
  1863. {
  1864. }
  1865. // Add the symbols to the symbol table.
  1866. template<int size, bool big_endian>
  1867. void
  1868. Sized_relobj_incr<size, big_endian>::do_add_symbols(
  1869. Symbol_table* symtab,
  1870. Read_symbols_data*,
  1871. Layout*)
  1872. {
  1873. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1874. unsigned char symbuf[sym_size];
  1875. elfcpp::Sym<size, big_endian> sym(symbuf);
  1876. elfcpp::Sym_write<size, big_endian> osym(symbuf);
  1877. typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
  1878. unsigned int nsyms = this->input_reader_.get_global_symbol_count();
  1879. this->symbols_.resize(nsyms);
  1880. Incremental_binary::View symtab_view(NULL);
  1881. unsigned int symtab_count;
  1882. elfcpp::Elf_strtab strtab(NULL, 0);
  1883. this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  1884. Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
  1885. unsigned int isym_count = isymtab.symbol_count();
  1886. unsigned int first_global = symtab_count - isym_count;
  1887. const unsigned char* sym_p;
  1888. for (unsigned int i = 0; i < nsyms; ++i)
  1889. {
  1890. Incremental_global_symbol_reader<big_endian> info =
  1891. this->input_reader_.get_global_symbol_reader(i);
  1892. unsigned int output_symndx = info.output_symndx();
  1893. sym_p = symtab_view.data() + output_symndx * sym_size;
  1894. elfcpp::Sym<size, big_endian> gsym(sym_p);
  1895. const char* name;
  1896. if (!strtab.get_c_string(gsym.get_st_name(), &name))
  1897. name = "";
  1898. typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
  1899. unsigned int shndx = gsym.get_st_shndx();
  1900. elfcpp::STB st_bind = gsym.get_st_bind();
  1901. elfcpp::STT st_type = gsym.get_st_type();
  1902. // Local hidden symbols start out as globals, but get converted to
  1903. // to local during output.
  1904. if (st_bind == elfcpp::STB_LOCAL)
  1905. st_bind = elfcpp::STB_GLOBAL;
  1906. unsigned int input_shndx = info.shndx();
  1907. if (input_shndx == 0 || input_shndx == -1U)
  1908. {
  1909. shndx = elfcpp::SHN_UNDEF;
  1910. v = 0;
  1911. }
  1912. else if (shndx != elfcpp::SHN_ABS)
  1913. {
  1914. // Find the input section and calculate the section-relative value.
  1915. gold_assert(shndx != elfcpp::SHN_UNDEF);
  1916. Output_section* os = this->ibase_->output_section(shndx);
  1917. gold_assert(os != NULL && os->has_fixed_layout());
  1918. typename Input_entry_reader::Input_section_info sect =
  1919. this->input_reader_.get_input_section(input_shndx - 1);
  1920. gold_assert(sect.output_shndx == shndx);
  1921. if (st_type != elfcpp::STT_TLS)
  1922. v -= os->address();
  1923. v -= sect.sh_offset;
  1924. shndx = input_shndx;
  1925. }
  1926. osym.put_st_name(0);
  1927. osym.put_st_value(v);
  1928. osym.put_st_size(gsym.get_st_size());
  1929. osym.put_st_info(st_bind, st_type);
  1930. osym.put_st_other(gsym.get_st_other());
  1931. osym.put_st_shndx(shndx);
  1932. Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
  1933. if (shndx != elfcpp::SHN_UNDEF)
  1934. ++this->defined_count_;
  1935. // If this is a linker-defined symbol that hasn't yet been defined,
  1936. // define it now.
  1937. if (input_shndx == -1U && !res->is_defined())
  1938. {
  1939. shndx = gsym.get_st_shndx();
  1940. v = gsym.get_st_value();
  1941. Elf_size_type symsize = gsym.get_st_size();
  1942. if (shndx == elfcpp::SHN_ABS)
  1943. {
  1944. symtab->define_as_constant(name, NULL,
  1945. Symbol_table::INCREMENTAL_BASE,
  1946. v, symsize, st_type, st_bind,
  1947. gsym.get_st_visibility(), 0,
  1948. false, false);
  1949. }
  1950. else
  1951. {
  1952. Output_section* os = this->ibase_->output_section(shndx);
  1953. gold_assert(os != NULL && os->has_fixed_layout());
  1954. v -= os->address();
  1955. if (symsize > 0)
  1956. os->reserve(v, symsize);
  1957. symtab->define_in_output_data(name, NULL,
  1958. Symbol_table::INCREMENTAL_BASE,
  1959. os, v, symsize, st_type, st_bind,
  1960. gsym.get_st_visibility(), 0,
  1961. false, false);
  1962. }
  1963. }
  1964. this->symbols_[i] = res;
  1965. this->ibase_->add_global_symbol(output_symndx - first_global, res);
  1966. }
  1967. }
  1968. // Return TRUE if we should include this object from an archive library.
  1969. template<int size, bool big_endian>
  1970. Archive::Should_include
  1971. Sized_relobj_incr<size, big_endian>::do_should_include_member(
  1972. Symbol_table*,
  1973. Layout*,
  1974. Read_symbols_data*,
  1975. std::string*)
  1976. {
  1977. gold_unreachable();
  1978. }
  1979. // Iterate over global symbols, calling a visitor class V for each.
  1980. template<int size, bool big_endian>
  1981. void
  1982. Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
  1983. Read_symbols_data*,
  1984. Library_base::Symbol_visitor_base*)
  1985. {
  1986. // This routine is not used for incremental objects.
  1987. }
  1988. // Get the size of a section.
  1989. template<int size, bool big_endian>
  1990. uint64_t
  1991. Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
  1992. {
  1993. gold_unreachable();
  1994. }
  1995. // Get the name of a section. This returns the name of the output
  1996. // section, because we don't usually track the names of the input
  1997. // sections.
  1998. template<int size, bool big_endian>
  1999. std::string
  2000. Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int shndx) const
  2001. {
  2002. const Output_sections& out_sections(this->output_sections());
  2003. const Output_section* os = out_sections[shndx];
  2004. if (os == NULL)
  2005. return NULL;
  2006. return os->name();
  2007. }
  2008. // Return a view of the contents of a section.
  2009. template<int size, bool big_endian>
  2010. const unsigned char*
  2011. Sized_relobj_incr<size, big_endian>::do_section_contents(
  2012. unsigned int shndx,
  2013. section_size_type* plen,
  2014. bool)
  2015. {
  2016. Output_sections& out_sections(this->output_sections());
  2017. Output_section* os = out_sections[shndx];
  2018. gold_assert(os != NULL);
  2019. off_t section_offset = os->offset();
  2020. typename Input_entry_reader::Input_section_info sect =
  2021. this->input_reader_.get_input_section(shndx - 1);
  2022. section_offset += sect.sh_offset;
  2023. *plen = sect.sh_size;
  2024. return this->ibase_->view(section_offset, sect.sh_size).data();
  2025. }
  2026. // Return section flags.
  2027. template<int size, bool big_endian>
  2028. uint64_t
  2029. Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
  2030. {
  2031. gold_unreachable();
  2032. }
  2033. // Return section entsize.
  2034. template<int size, bool big_endian>
  2035. uint64_t
  2036. Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
  2037. {
  2038. gold_unreachable();
  2039. }
  2040. // Return section address.
  2041. template<int size, bool big_endian>
  2042. uint64_t
  2043. Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
  2044. {
  2045. gold_unreachable();
  2046. }
  2047. // Return section type.
  2048. template<int size, bool big_endian>
  2049. unsigned int
  2050. Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
  2051. {
  2052. gold_unreachable();
  2053. }
  2054. // Return the section link field.
  2055. template<int size, bool big_endian>
  2056. unsigned int
  2057. Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
  2058. {
  2059. gold_unreachable();
  2060. }
  2061. // Return the section link field.
  2062. template<int size, bool big_endian>
  2063. unsigned int
  2064. Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
  2065. {
  2066. gold_unreachable();
  2067. }
  2068. // Return the section alignment.
  2069. template<int size, bool big_endian>
  2070. uint64_t
  2071. Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
  2072. {
  2073. gold_unreachable();
  2074. }
  2075. // Return the Xindex structure to use.
  2076. template<int size, bool big_endian>
  2077. Xindex*
  2078. Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
  2079. {
  2080. gold_unreachable();
  2081. }
  2082. // Get symbol counts.
  2083. template<int size, bool big_endian>
  2084. void
  2085. Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
  2086. const Symbol_table*,
  2087. size_t* defined,
  2088. size_t* used) const
  2089. {
  2090. *defined = this->defined_count_;
  2091. size_t count = 0;
  2092. for (typename Symbols::const_iterator p = this->symbols_.begin();
  2093. p != this->symbols_.end();
  2094. ++p)
  2095. if (*p != NULL
  2096. && (*p)->source() == Symbol::FROM_OBJECT
  2097. && (*p)->object() == this
  2098. && (*p)->is_defined())
  2099. ++count;
  2100. *used = count;
  2101. }
  2102. // Read the relocs.
  2103. template<int size, bool big_endian>
  2104. void
  2105. Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
  2106. {
  2107. }
  2108. // Process the relocs to find list of referenced sections. Used only
  2109. // during garbage collection.
  2110. template<int size, bool big_endian>
  2111. void
  2112. Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
  2113. Layout*,
  2114. Read_relocs_data*)
  2115. {
  2116. gold_unreachable();
  2117. }
  2118. // Scan the relocs and adjust the symbol table.
  2119. template<int size, bool big_endian>
  2120. void
  2121. Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
  2122. Layout* layout,
  2123. Read_relocs_data*)
  2124. {
  2125. // Count the incremental relocations for this object.
  2126. unsigned int nsyms = this->input_reader_.get_global_symbol_count();
  2127. this->allocate_incremental_reloc_counts();
  2128. for (unsigned int i = 0; i < nsyms; i++)
  2129. {
  2130. Incremental_global_symbol_reader<big_endian> sym =
  2131. this->input_reader_.get_global_symbol_reader(i);
  2132. unsigned int reloc_count = sym.reloc_count();
  2133. if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
  2134. this->incr_reloc_offset_ = sym.reloc_offset();
  2135. this->incr_reloc_count_ += reloc_count;
  2136. for (unsigned int j = 0; j < reloc_count; j++)
  2137. this->count_incremental_reloc(i);
  2138. }
  2139. this->incr_reloc_output_index_ =
  2140. layout->incremental_inputs()->get_reloc_count();
  2141. this->finalize_incremental_relocs(layout, false);
  2142. // The incoming incremental relocations may not end up in the same
  2143. // location after the incremental update, because the incremental info
  2144. // is regenerated in each link. Because the new location may overlap
  2145. // with other data in the updated output file, we need to copy the
  2146. // relocations into a buffer so that we can still read them safely
  2147. // after we start writing updates to the output file.
  2148. if (this->incr_reloc_count_ > 0)
  2149. {
  2150. const Incremental_relocs_reader<size, big_endian>& relocs_reader =
  2151. this->ibase_->relocs_reader();
  2152. const unsigned int incr_reloc_size = relocs_reader.reloc_size;
  2153. unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
  2154. this->incr_relocs_ = new unsigned char[len];
  2155. memcpy(this->incr_relocs_,
  2156. relocs_reader.data(this->incr_reloc_offset_),
  2157. len);
  2158. }
  2159. }
  2160. // Count the local symbols.
  2161. template<int size, bool big_endian>
  2162. void
  2163. Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
  2164. Stringpool_template<char>* pool,
  2165. Stringpool_template<char>*)
  2166. {
  2167. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2168. // Set the count of local symbols based on the incremental info.
  2169. unsigned int nlocals = this->input_reader_.get_local_symbol_count();
  2170. this->local_symbol_count_ = nlocals;
  2171. this->local_symbols_.reserve(nlocals);
  2172. // Get views of the base file's symbol table and string table.
  2173. Incremental_binary::View symtab_view(NULL);
  2174. unsigned int symtab_count;
  2175. elfcpp::Elf_strtab strtab(NULL, 0);
  2176. this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  2177. // Read the local symbols from the base file's symbol table.
  2178. off_t off = this->input_reader_.get_local_symbol_offset();
  2179. const unsigned char* symp = symtab_view.data() + off;
  2180. for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
  2181. {
  2182. elfcpp::Sym<size, big_endian> sym(symp);
  2183. const char* name;
  2184. if (!strtab.get_c_string(sym.get_st_name(), &name))
  2185. name = "";
  2186. gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
  2187. name = pool->add(name, true, NULL);
  2188. this->local_symbols_.push_back(Local_symbol(name,
  2189. sym.get_st_value(),
  2190. sym.get_st_size(),
  2191. sym.get_st_shndx(),
  2192. sym.get_st_type(),
  2193. false));
  2194. }
  2195. }
  2196. // Finalize the local symbols.
  2197. template<int size, bool big_endian>
  2198. unsigned int
  2199. Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
  2200. unsigned int index,
  2201. off_t off,
  2202. Symbol_table*)
  2203. {
  2204. this->local_symbol_index_ = index;
  2205. this->local_symbol_offset_ = off;
  2206. return index + this->local_symbol_count_;
  2207. }
  2208. // Set the offset where local dynamic symbol information will be stored.
  2209. template<int size, bool big_endian>
  2210. unsigned int
  2211. Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
  2212. unsigned int index)
  2213. {
  2214. // FIXME: set local dynsym indexes.
  2215. return index;
  2216. }
  2217. // Set the offset where local dynamic symbol information will be stored.
  2218. template<int size, bool big_endian>
  2219. unsigned int
  2220. Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
  2221. {
  2222. return 0;
  2223. }
  2224. // Relocate the input sections and write out the local symbols.
  2225. // We don't actually do any relocation here. For unchanged input files,
  2226. // we reapply relocations only for symbols that have changed; that happens
  2227. // in queue_final_tasks. We do need to rewrite the incremental relocations
  2228. // for this object.
  2229. template<int size, bool big_endian>
  2230. void
  2231. Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
  2232. const Layout* layout,
  2233. Output_file* of)
  2234. {
  2235. if (this->incr_reloc_count_ == 0)
  2236. return;
  2237. const unsigned int incr_reloc_size =
  2238. Incremental_relocs_reader<size, big_endian>::reloc_size;
  2239. // Get a view for the .gnu_incremental_relocs section.
  2240. Incremental_inputs* inputs = layout->incremental_inputs();
  2241. gold_assert(inputs != NULL);
  2242. const off_t relocs_off = inputs->relocs_section()->offset();
  2243. const off_t relocs_size = inputs->relocs_section()->data_size();
  2244. unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
  2245. // Copy the relocations from the buffer.
  2246. off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
  2247. unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
  2248. memcpy(view + off, this->incr_relocs_, len);
  2249. // The output section table may have changed, so we need to map
  2250. // the old section index to the new section index for each relocation.
  2251. for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
  2252. {
  2253. unsigned char* pov = view + off + i * incr_reloc_size;
  2254. unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
  2255. Output_section* os = this->ibase_->output_section(shndx);
  2256. gold_assert(os != NULL);
  2257. shndx = os->out_shndx();
  2258. elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
  2259. }
  2260. of->write_output_view(off, len, view);
  2261. // Get views into the output file for the portions of the symbol table
  2262. // and the dynamic symbol table that we will be writing.
  2263. off_t symtab_off = layout->symtab_section()->offset();
  2264. off_t output_size = this->local_symbol_count_ * This::sym_size;
  2265. unsigned char* oview = NULL;
  2266. if (output_size > 0)
  2267. oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
  2268. output_size);
  2269. off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
  2270. unsigned char* dyn_oview = NULL;
  2271. if (dyn_output_size > 0)
  2272. dyn_oview = of->get_output_view(this->local_dynsym_offset_,
  2273. dyn_output_size);
  2274. // Write the local symbols.
  2275. unsigned char* ov = oview;
  2276. unsigned char* dyn_ov = dyn_oview;
  2277. const Stringpool* sympool = layout->sympool();
  2278. const Stringpool* dynpool = layout->dynpool();
  2279. Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
  2280. Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
  2281. for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
  2282. {
  2283. Local_symbol& lsym(this->local_symbols_[i]);
  2284. bool is_ordinary;
  2285. unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
  2286. &is_ordinary);
  2287. if (is_ordinary)
  2288. {
  2289. Output_section* os = this->ibase_->output_section(st_shndx);
  2290. st_shndx = os->out_shndx();
  2291. if (st_shndx >= elfcpp::SHN_LORESERVE)
  2292. {
  2293. symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
  2294. if (lsym.needs_dynsym_entry)
  2295. dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
  2296. st_shndx = elfcpp::SHN_XINDEX;
  2297. }
  2298. }
  2299. // Write the symbol to the output symbol table.
  2300. {
  2301. elfcpp::Sym_write<size, big_endian> osym(ov);
  2302. osym.put_st_name(sympool->get_offset(lsym.name));
  2303. osym.put_st_value(lsym.st_value);
  2304. osym.put_st_size(lsym.st_size);
  2305. osym.put_st_info(elfcpp::STB_LOCAL,
  2306. static_cast<elfcpp::STT>(lsym.st_type));
  2307. osym.put_st_other(0);
  2308. osym.put_st_shndx(st_shndx);
  2309. ov += sym_size;
  2310. }
  2311. // Write the symbol to the output dynamic symbol table.
  2312. if (lsym.needs_dynsym_entry)
  2313. {
  2314. gold_assert(dyn_ov < dyn_oview + dyn_output_size);
  2315. elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
  2316. osym.put_st_name(dynpool->get_offset(lsym.name));
  2317. osym.put_st_value(lsym.st_value);
  2318. osym.put_st_size(lsym.st_size);
  2319. osym.put_st_info(elfcpp::STB_LOCAL,
  2320. static_cast<elfcpp::STT>(lsym.st_type));
  2321. osym.put_st_other(0);
  2322. osym.put_st_shndx(st_shndx);
  2323. dyn_ov += sym_size;
  2324. }
  2325. }
  2326. if (output_size > 0)
  2327. {
  2328. gold_assert(ov - oview == output_size);
  2329. of->write_output_view(symtab_off + this->local_symbol_offset_,
  2330. output_size, oview);
  2331. }
  2332. if (dyn_output_size > 0)
  2333. {
  2334. gold_assert(dyn_ov - dyn_oview == dyn_output_size);
  2335. of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
  2336. dyn_oview);
  2337. }
  2338. }
  2339. // Set the offset of a section.
  2340. template<int size, bool big_endian>
  2341. void
  2342. Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
  2343. uint64_t)
  2344. {
  2345. }
  2346. // Class Sized_incr_dynobj. Most of these methods are not used for
  2347. // Incremental objects, but are required to be implemented by the
  2348. // base class Object.
  2349. template<int size, bool big_endian>
  2350. Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
  2351. const std::string& name,
  2352. Sized_incremental_binary<size, big_endian>* ibase,
  2353. unsigned int input_file_index)
  2354. : Dynobj(name, NULL), ibase_(ibase),
  2355. input_file_index_(input_file_index),
  2356. input_reader_(ibase->inputs_reader().input_file(input_file_index)),
  2357. symbols_(), defined_count_(0)
  2358. {
  2359. if (this->input_reader_.is_in_system_directory())
  2360. this->set_is_in_system_directory();
  2361. if (this->input_reader_.as_needed())
  2362. this->set_as_needed();
  2363. this->set_soname_string(this->input_reader_.get_soname());
  2364. this->set_shnum(0);
  2365. }
  2366. // Read the symbols.
  2367. template<int size, bool big_endian>
  2368. void
  2369. Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
  2370. {
  2371. gold_unreachable();
  2372. }
  2373. // Lay out the input sections.
  2374. template<int size, bool big_endian>
  2375. void
  2376. Sized_incr_dynobj<size, big_endian>::do_layout(
  2377. Symbol_table*,
  2378. Layout*,
  2379. Read_symbols_data*)
  2380. {
  2381. }
  2382. // Add the symbols to the symbol table.
  2383. template<int size, bool big_endian>
  2384. void
  2385. Sized_incr_dynobj<size, big_endian>::do_add_symbols(
  2386. Symbol_table* symtab,
  2387. Read_symbols_data*,
  2388. Layout*)
  2389. {
  2390. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2391. unsigned char symbuf[sym_size];
  2392. elfcpp::Sym<size, big_endian> sym(symbuf);
  2393. elfcpp::Sym_write<size, big_endian> osym(symbuf);
  2394. unsigned int nsyms = this->input_reader_.get_global_symbol_count();
  2395. this->symbols_.resize(nsyms);
  2396. Incremental_binary::View symtab_view(NULL);
  2397. unsigned int symtab_count;
  2398. elfcpp::Elf_strtab strtab(NULL, 0);
  2399. this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
  2400. Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
  2401. unsigned int isym_count = isymtab.symbol_count();
  2402. unsigned int first_global = symtab_count - isym_count;
  2403. // We keep a set of symbols that we have generated COPY relocations
  2404. // for, indexed by the symbol value. We do not need more than one
  2405. // COPY relocation per address.
  2406. typedef typename std::set<Address> Copied_symbols;
  2407. Copied_symbols copied_symbols;
  2408. const unsigned char* sym_p;
  2409. for (unsigned int i = 0; i < nsyms; ++i)
  2410. {
  2411. bool is_def;
  2412. bool is_copy;
  2413. unsigned int output_symndx =
  2414. this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
  2415. sym_p = symtab_view.data() + output_symndx * sym_size;
  2416. elfcpp::Sym<size, big_endian> gsym(sym_p);
  2417. const char* name;
  2418. if (!strtab.get_c_string(gsym.get_st_name(), &name))
  2419. name = "";
  2420. Address v;
  2421. unsigned int shndx;
  2422. elfcpp::STB st_bind = gsym.get_st_bind();
  2423. elfcpp::STT st_type = gsym.get_st_type();
  2424. // Local hidden symbols start out as globals, but get converted to
  2425. // to local during output.
  2426. if (st_bind == elfcpp::STB_LOCAL)
  2427. st_bind = elfcpp::STB_GLOBAL;
  2428. if (!is_def)
  2429. {
  2430. shndx = elfcpp::SHN_UNDEF;
  2431. v = 0;
  2432. }
  2433. else
  2434. {
  2435. // For a symbol defined in a shared object, the section index
  2436. // is meaningless, as long as it's not SHN_UNDEF.
  2437. shndx = 1;
  2438. v = gsym.get_st_value();
  2439. ++this->defined_count_;
  2440. }
  2441. osym.put_st_name(0);
  2442. osym.put_st_value(v);
  2443. osym.put_st_size(gsym.get_st_size());
  2444. osym.put_st_info(st_bind, st_type);
  2445. osym.put_st_other(gsym.get_st_other());
  2446. osym.put_st_shndx(shndx);
  2447. Sized_symbol<size>* res =
  2448. symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
  2449. this->symbols_[i] = res;
  2450. this->ibase_->add_global_symbol(output_symndx - first_global,
  2451. this->symbols_[i]);
  2452. if (is_copy)
  2453. {
  2454. std::pair<typename Copied_symbols::iterator, bool> ins =
  2455. copied_symbols.insert(v);
  2456. if (ins.second)
  2457. {
  2458. unsigned int shndx = gsym.get_st_shndx();
  2459. Output_section* os = this->ibase_->output_section(shndx);
  2460. off_t offset = v - os->address();
  2461. this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
  2462. }
  2463. }
  2464. }
  2465. }
  2466. // Return TRUE if we should include this object from an archive library.
  2467. template<int size, bool big_endian>
  2468. Archive::Should_include
  2469. Sized_incr_dynobj<size, big_endian>::do_should_include_member(
  2470. Symbol_table*,
  2471. Layout*,
  2472. Read_symbols_data*,
  2473. std::string*)
  2474. {
  2475. gold_unreachable();
  2476. }
  2477. // Iterate over global symbols, calling a visitor class V for each.
  2478. template<int size, bool big_endian>
  2479. void
  2480. Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
  2481. Read_symbols_data*,
  2482. Library_base::Symbol_visitor_base*)
  2483. {
  2484. // This routine is not used for dynamic libraries.
  2485. }
  2486. // Iterate over local symbols, calling a visitor class V for each GOT offset
  2487. // associated with a local symbol.
  2488. template<int size, bool big_endian>
  2489. void
  2490. Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
  2491. Got_offset_list::Visitor*) const
  2492. {
  2493. }
  2494. // Get the size of a section.
  2495. template<int size, bool big_endian>
  2496. uint64_t
  2497. Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
  2498. {
  2499. gold_unreachable();
  2500. }
  2501. // Get the name of a section.
  2502. template<int size, bool big_endian>
  2503. std::string
  2504. Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int) const
  2505. {
  2506. gold_unreachable();
  2507. }
  2508. // Return a view of the contents of a section.
  2509. template<int size, bool big_endian>
  2510. const unsigned char*
  2511. Sized_incr_dynobj<size, big_endian>::do_section_contents(
  2512. unsigned int,
  2513. section_size_type*,
  2514. bool)
  2515. {
  2516. gold_unreachable();
  2517. }
  2518. // Return section flags.
  2519. template<int size, bool big_endian>
  2520. uint64_t
  2521. Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
  2522. {
  2523. gold_unreachable();
  2524. }
  2525. // Return section entsize.
  2526. template<int size, bool big_endian>
  2527. uint64_t
  2528. Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
  2529. {
  2530. gold_unreachable();
  2531. }
  2532. // Return section address.
  2533. template<int size, bool big_endian>
  2534. uint64_t
  2535. Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
  2536. {
  2537. gold_unreachable();
  2538. }
  2539. // Return section type.
  2540. template<int size, bool big_endian>
  2541. unsigned int
  2542. Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
  2543. {
  2544. gold_unreachable();
  2545. }
  2546. // Return the section link field.
  2547. template<int size, bool big_endian>
  2548. unsigned int
  2549. Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
  2550. {
  2551. gold_unreachable();
  2552. }
  2553. // Return the section link field.
  2554. template<int size, bool big_endian>
  2555. unsigned int
  2556. Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
  2557. {
  2558. gold_unreachable();
  2559. }
  2560. // Return the section alignment.
  2561. template<int size, bool big_endian>
  2562. uint64_t
  2563. Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
  2564. {
  2565. gold_unreachable();
  2566. }
  2567. // Return the Xindex structure to use.
  2568. template<int size, bool big_endian>
  2569. Xindex*
  2570. Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
  2571. {
  2572. gold_unreachable();
  2573. }
  2574. // Get symbol counts.
  2575. template<int size, bool big_endian>
  2576. void
  2577. Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
  2578. const Symbol_table*,
  2579. size_t* defined,
  2580. size_t* used) const
  2581. {
  2582. *defined = this->defined_count_;
  2583. size_t count = 0;
  2584. for (typename Symbols::const_iterator p = this->symbols_.begin();
  2585. p != this->symbols_.end();
  2586. ++p)
  2587. if (*p != NULL
  2588. && (*p)->source() == Symbol::FROM_OBJECT
  2589. && (*p)->object() == this
  2590. && (*p)->is_defined()
  2591. && (*p)->dynsym_index() != -1U)
  2592. ++count;
  2593. *used = count;
  2594. }
  2595. // Allocate an incremental object of the appropriate size and endianness.
  2596. Object*
  2597. make_sized_incremental_object(
  2598. Incremental_binary* ibase,
  2599. unsigned int input_file_index,
  2600. Incremental_input_type input_type,
  2601. const Incremental_binary::Input_reader* input_reader)
  2602. {
  2603. Object* obj = NULL;
  2604. std::string name(input_reader->filename());
  2605. switch (parameters->size_and_endianness())
  2606. {
  2607. #ifdef HAVE_TARGET_32_LITTLE
  2608. case Parameters::TARGET_32_LITTLE:
  2609. {
  2610. Sized_incremental_binary<32, false>* sized_ibase =
  2611. static_cast<Sized_incremental_binary<32, false>*>(ibase);
  2612. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2613. obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
  2614. input_file_index);
  2615. else
  2616. obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
  2617. input_file_index);
  2618. }
  2619. break;
  2620. #endif
  2621. #ifdef HAVE_TARGET_32_BIG
  2622. case Parameters::TARGET_32_BIG:
  2623. {
  2624. Sized_incremental_binary<32, true>* sized_ibase =
  2625. static_cast<Sized_incremental_binary<32, true>*>(ibase);
  2626. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2627. obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
  2628. input_file_index);
  2629. else
  2630. obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
  2631. input_file_index);
  2632. }
  2633. break;
  2634. #endif
  2635. #ifdef HAVE_TARGET_64_LITTLE
  2636. case Parameters::TARGET_64_LITTLE:
  2637. {
  2638. Sized_incremental_binary<64, false>* sized_ibase =
  2639. static_cast<Sized_incremental_binary<64, false>*>(ibase);
  2640. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2641. obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
  2642. input_file_index);
  2643. else
  2644. obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
  2645. input_file_index);
  2646. }
  2647. break;
  2648. #endif
  2649. #ifdef HAVE_TARGET_64_BIG
  2650. case Parameters::TARGET_64_BIG:
  2651. {
  2652. Sized_incremental_binary<64, true>* sized_ibase =
  2653. static_cast<Sized_incremental_binary<64, true>*>(ibase);
  2654. if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
  2655. obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
  2656. input_file_index);
  2657. else
  2658. obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
  2659. input_file_index);
  2660. }
  2661. break;
  2662. #endif
  2663. default:
  2664. gold_unreachable();
  2665. }
  2666. gold_assert(obj != NULL);
  2667. return obj;
  2668. }
  2669. // Copy the unused symbols from the incremental input info.
  2670. // We need to do this because we may be overwriting the incremental
  2671. // input info in the base file before we write the new incremental
  2672. // info.
  2673. void
  2674. Incremental_library::copy_unused_symbols()
  2675. {
  2676. unsigned int symcount = this->input_reader_->get_unused_symbol_count();
  2677. this->unused_symbols_.reserve(symcount);
  2678. for (unsigned int i = 0; i < symcount; ++i)
  2679. {
  2680. std::string name(this->input_reader_->get_unused_symbol(i));
  2681. this->unused_symbols_.push_back(name);
  2682. }
  2683. }
  2684. // Iterator for unused global symbols in the library.
  2685. void
  2686. Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
  2687. {
  2688. for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
  2689. p != this->unused_symbols_.end();
  2690. ++p)
  2691. v->visit(p->c_str());
  2692. }
  2693. // Instantiate the templates we need.
  2694. #ifdef HAVE_TARGET_32_LITTLE
  2695. template
  2696. class Sized_incremental_binary<32, false>;
  2697. template
  2698. class Sized_relobj_incr<32, false>;
  2699. template
  2700. class Sized_incr_dynobj<32, false>;
  2701. #endif
  2702. #ifdef HAVE_TARGET_32_BIG
  2703. template
  2704. class Sized_incremental_binary<32, true>;
  2705. template
  2706. class Sized_relobj_incr<32, true>;
  2707. template
  2708. class Sized_incr_dynobj<32, true>;
  2709. #endif
  2710. #ifdef HAVE_TARGET_64_LITTLE
  2711. template
  2712. class Sized_incremental_binary<64, false>;
  2713. template
  2714. class Sized_relobj_incr<64, false>;
  2715. template
  2716. class Sized_incr_dynobj<64, false>;
  2717. #endif
  2718. #ifdef HAVE_TARGET_64_BIG
  2719. template
  2720. class Sized_incremental_binary<64, true>;
  2721. template
  2722. class Sized_relobj_incr<64, true>;
  2723. template
  2724. class Sized_incr_dynobj<64, true>;
  2725. #endif
  2726. } // End namespace gold.