dwp.cc 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  1. // dwp.cc -- DWARF packaging utility
  2. // Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. // Written by Cary Coutant <ccoutant@google.com>.
  4. // This file is part of dwp, the DWARF packaging utility.
  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 "dwp.h"
  18. #include <cstdarg>
  19. #include <cstddef>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <cerrno>
  24. #include <vector>
  25. #include <algorithm>
  26. #include "getopt.h"
  27. #include "libiberty.h"
  28. #include "../bfd/bfdver.h"
  29. #include "elfcpp.h"
  30. #include "elfcpp_file.h"
  31. #include "dwarf.h"
  32. #include "dirsearch.h"
  33. #include "fileread.h"
  34. #include "object.h"
  35. #include "compressed_output.h"
  36. #include "stringpool.h"
  37. #include "dwarf_reader.h"
  38. static void
  39. usage(FILE* fd, int) ATTRIBUTE_NORETURN;
  40. static void
  41. print_version() ATTRIBUTE_NORETURN;
  42. namespace gold {
  43. class Dwp_output_file;
  44. template <int size, bool big_endian>
  45. class Sized_relobj_dwo;
  46. // List of .dwo files to process.
  47. struct Dwo_file_entry
  48. {
  49. Dwo_file_entry(uint64_t id, std::string name)
  50. : dwo_id(id), dwo_name(name)
  51. { }
  52. uint64_t dwo_id;
  53. std::string dwo_name;
  54. };
  55. typedef std::vector<Dwo_file_entry> File_list;
  56. // Type to hold the offset and length of an input section
  57. // within an output section.
  58. struct Section_bounds
  59. {
  60. section_offset_type offset;
  61. section_size_type size;
  62. Section_bounds()
  63. : offset(0), size(0)
  64. { }
  65. Section_bounds(section_offset_type o, section_size_type s)
  66. : offset(o), size(s)
  67. { }
  68. };
  69. // A set of sections for a compilation unit or type unit.
  70. struct Unit_set
  71. {
  72. uint64_t signature;
  73. Section_bounds sections[elfcpp::DW_SECT_MAX + 1];
  74. Unit_set()
  75. : signature(0), sections()
  76. { }
  77. };
  78. // An input file.
  79. // This class may represent a .dwo file, a .dwp file
  80. // produced by an earlier run, or an executable file whose
  81. // debug section identifies a set of .dwo files to read.
  82. class Dwo_file
  83. {
  84. public:
  85. Dwo_file(const char* name)
  86. : name_(name), obj_(NULL), input_file_(NULL), is_compressed_(),
  87. sect_offsets_(), str_offset_map_()
  88. { }
  89. ~Dwo_file();
  90. // Read the input executable file and extract the list of .dwo files
  91. // that it references.
  92. void
  93. read_executable(File_list* files);
  94. // Read the input file and send its contents to OUTPUT_FILE.
  95. void
  96. read(Dwp_output_file* output_file);
  97. // Verify a .dwp file given a list of .dwo files referenced by the
  98. // corresponding executable file. Returns true if no problems
  99. // were found.
  100. bool
  101. verify(const File_list& files);
  102. private:
  103. // Types for mapping input string offsets to output string offsets.
  104. typedef std::pair<section_offset_type, section_offset_type>
  105. Str_offset_map_entry;
  106. typedef std::vector<Str_offset_map_entry> Str_offset_map;
  107. // A less-than comparison routine for Str_offset_map.
  108. struct Offset_compare
  109. {
  110. bool
  111. operator()(const Str_offset_map_entry& i1,
  112. const Str_offset_map_entry& i2) const
  113. { return i1.first < i2.first; }
  114. };
  115. // Create a Sized_relobj_dwo of the given size and endianness,
  116. // and record the target info. P is a pointer to the ELF header
  117. // in memory.
  118. Relobj*
  119. make_object(Dwp_output_file* output_file);
  120. template <int size, bool big_endian>
  121. Relobj*
  122. sized_make_object(const unsigned char* p, Input_file* input_file,
  123. Dwp_output_file* output_file);
  124. // Return the number of sections in the input object file.
  125. unsigned int
  126. shnum() const
  127. { return this->obj_->shnum(); }
  128. // Return section type.
  129. unsigned int
  130. section_type(unsigned int shndx)
  131. { return this->obj_->section_type(shndx); }
  132. // Get the name of a section.
  133. std::string
  134. section_name(unsigned int shndx)
  135. { return this->obj_->section_name(shndx); }
  136. // Return a view of the contents of a section, decompressed if necessary.
  137. // Set *PLEN to the size. Set *IS_NEW to true if the contents need to be
  138. // deleted by the caller.
  139. const unsigned char*
  140. section_contents(unsigned int shndx, section_size_type* plen, bool* is_new)
  141. { return this->obj_->decompressed_section_contents(shndx, plen, is_new); }
  142. // Read the .debug_cu_index or .debug_tu_index section of a .dwp file,
  143. // and process the CU or TU sets.
  144. void
  145. read_unit_index(unsigned int, unsigned int *, Dwp_output_file*,
  146. bool is_tu_index);
  147. template <bool big_endian>
  148. void
  149. sized_read_unit_index(unsigned int, unsigned int *, Dwp_output_file*,
  150. bool is_tu_index);
  151. // Verify the .debug_cu_index section of a .dwp file, comparing it
  152. // against the list of .dwo files referenced by the corresponding
  153. // executable file.
  154. bool
  155. verify_dwo_list(unsigned int, const File_list& files);
  156. template <bool big_endian>
  157. bool
  158. sized_verify_dwo_list(unsigned int, const File_list& files);
  159. // Merge the input string table section into the output file.
  160. void
  161. add_strings(Dwp_output_file*, unsigned int);
  162. // Copy a section from the input file to the output file.
  163. Section_bounds
  164. copy_section(Dwp_output_file* output_file, unsigned int shndx,
  165. elfcpp::DW_SECT section_id);
  166. // Remap the string offsets in the .debug_str_offsets.dwo section.
  167. const unsigned char*
  168. remap_str_offsets(const unsigned char* contents, section_size_type len);
  169. template <bool big_endian>
  170. const unsigned char*
  171. sized_remap_str_offsets(const unsigned char* contents, section_size_type len);
  172. // Remap a single string offsets from an offset in the input string table
  173. // to an offset in the output string table.
  174. unsigned int
  175. remap_str_offset(section_offset_type val);
  176. // Add a set of .debug_info.dwo or .debug_types.dwo and related sections
  177. // to OUTPUT_FILE.
  178. void
  179. add_unit_set(Dwp_output_file* output_file, unsigned int *debug_shndx,
  180. bool is_debug_types);
  181. // The filename.
  182. const char* name_;
  183. // The ELF file, represented as a gold Relobj instance.
  184. Relobj* obj_;
  185. // The Input_file object.
  186. Input_file* input_file_;
  187. // Flags indicating which sections are compressed.
  188. std::vector<bool> is_compressed_;
  189. // Map input section index onto output section offset and size.
  190. std::vector<Section_bounds> sect_offsets_;
  191. // Map input string offsets to output string offsets.
  192. Str_offset_map str_offset_map_;
  193. };
  194. // An ELF input file.
  195. // We derive from Sized_relobj so that we can use interfaces
  196. // in libgold to access the file.
  197. template <int size, bool big_endian>
  198. class Sized_relobj_dwo : public Sized_relobj<size, big_endian>
  199. {
  200. public:
  201. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  202. typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
  203. Sized_relobj_dwo(const char* name, Input_file* input_file,
  204. const elfcpp::Ehdr<size, big_endian>& ehdr)
  205. : Sized_relobj<size, big_endian>(name, input_file),
  206. elf_file_(this, ehdr)
  207. { }
  208. ~Sized_relobj_dwo()
  209. { }
  210. // Setup the section information.
  211. void
  212. setup();
  213. protected:
  214. // Return section type.
  215. unsigned int
  216. do_section_type(unsigned int shndx)
  217. { return this->elf_file_.section_type(shndx); }
  218. // Get the name of a section.
  219. std::string
  220. do_section_name(unsigned int shndx) const
  221. { return this->elf_file_.section_name(shndx); }
  222. // Get the size of a section.
  223. uint64_t
  224. do_section_size(unsigned int shndx)
  225. { return this->elf_file_.section_size(shndx); }
  226. // Return a view of the contents of a section.
  227. const unsigned char*
  228. do_section_contents(unsigned int, section_size_type*, bool);
  229. // The following virtual functions are abstract in the base classes,
  230. // but are not used here.
  231. // Read the symbols.
  232. void
  233. do_read_symbols(Read_symbols_data*)
  234. { gold_unreachable(); }
  235. // Lay out the input sections.
  236. void
  237. do_layout(Symbol_table*, Layout*, Read_symbols_data*)
  238. { gold_unreachable(); }
  239. // Layout sections whose layout was deferred while waiting for
  240. // input files from a plugin.
  241. void
  242. do_layout_deferred_sections(Layout*)
  243. { gold_unreachable(); }
  244. // Add the symbols to the symbol table.
  245. void
  246. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*)
  247. { gold_unreachable(); }
  248. Archive::Should_include
  249. do_should_include_member(Symbol_table*, Layout*, Read_symbols_data*,
  250. std::string*)
  251. { gold_unreachable(); }
  252. // Iterate over global symbols, calling a visitor class V for each.
  253. void
  254. do_for_all_global_symbols(Read_symbols_data*,
  255. Library_base::Symbol_visitor_base*)
  256. { gold_unreachable(); }
  257. // Return section flags.
  258. uint64_t
  259. do_section_flags(unsigned int)
  260. { gold_unreachable(); }
  261. // Return section entsize.
  262. uint64_t
  263. do_section_entsize(unsigned int)
  264. { gold_unreachable(); }
  265. // Return section address.
  266. uint64_t
  267. do_section_address(unsigned int)
  268. { gold_unreachable(); }
  269. // Return the section link field.
  270. unsigned int
  271. do_section_link(unsigned int)
  272. { gold_unreachable(); }
  273. // Return the section link field.
  274. unsigned int
  275. do_section_info(unsigned int)
  276. { gold_unreachable(); }
  277. // Return the section alignment.
  278. uint64_t
  279. do_section_addralign(unsigned int)
  280. { gold_unreachable(); }
  281. // Return the Xindex structure to use.
  282. Xindex*
  283. do_initialize_xindex()
  284. { gold_unreachable(); }
  285. // Get symbol counts.
  286. void
  287. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const
  288. { gold_unreachable(); }
  289. // Get global symbols.
  290. const Symbols*
  291. do_get_global_symbols() const
  292. { return NULL; }
  293. // Return the value of a local symbol.
  294. uint64_t
  295. do_local_symbol_value(unsigned int, uint64_t) const
  296. { gold_unreachable(); }
  297. unsigned int
  298. do_local_plt_offset(unsigned int) const
  299. { gold_unreachable(); }
  300. // Return whether local symbol SYMNDX is a TLS symbol.
  301. bool
  302. do_local_is_tls(unsigned int) const
  303. { gold_unreachable(); }
  304. // Return the number of local symbols.
  305. unsigned int
  306. do_local_symbol_count() const
  307. { gold_unreachable(); }
  308. // Return the number of local symbols in the output symbol table.
  309. unsigned int
  310. do_output_local_symbol_count() const
  311. { gold_unreachable(); }
  312. // Return the file offset for local symbols in the output symbol table.
  313. off_t
  314. do_local_symbol_offset() const
  315. { gold_unreachable(); }
  316. // Read the relocs.
  317. void
  318. do_read_relocs(Read_relocs_data*)
  319. { gold_unreachable(); }
  320. // Process the relocs to find list of referenced sections. Used only
  321. // during garbage collection.
  322. void
  323. do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*)
  324. { gold_unreachable(); }
  325. // Scan the relocs and adjust the symbol table.
  326. void
  327. do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*)
  328. { gold_unreachable(); }
  329. // Count the local symbols.
  330. void
  331. do_count_local_symbols(Stringpool_template<char>*,
  332. Stringpool_template<char>*)
  333. { gold_unreachable(); }
  334. // Finalize the local symbols.
  335. unsigned int
  336. do_finalize_local_symbols(unsigned int, off_t, Symbol_table*)
  337. { gold_unreachable(); }
  338. // Set the offset where local dynamic symbol information will be stored.
  339. unsigned int
  340. do_set_local_dynsym_indexes(unsigned int)
  341. { gold_unreachable(); }
  342. // Set the offset where local dynamic symbol information will be stored.
  343. unsigned int
  344. do_set_local_dynsym_offset(off_t)
  345. { gold_unreachable(); }
  346. // Relocate the input sections and write out the local symbols.
  347. void
  348. do_relocate(const Symbol_table*, const Layout*, Output_file*)
  349. { gold_unreachable(); }
  350. private:
  351. // General access to the ELF file.
  352. elfcpp::Elf_file<size, big_endian, Object> elf_file_;
  353. };
  354. // The output file.
  355. // This class is responsible for collecting the debug index information
  356. // and writing the .dwp file in ELF format.
  357. class Dwp_output_file
  358. {
  359. public:
  360. Dwp_output_file(const char* name)
  361. : name_(name), machine_(0), size_(0), big_endian_(false), osabi_(0),
  362. abiversion_(0), fd_(NULL), next_file_offset_(0), shnum_(1), sections_(),
  363. section_id_map_(), shoff_(0), shstrndx_(0), have_strings_(false),
  364. stringpool_(), shstrtab_(), cu_index_(), tu_index_(), last_type_sig_(0),
  365. last_tu_slot_(0)
  366. {
  367. this->section_id_map_.resize(elfcpp::DW_SECT_MAX + 1);
  368. this->stringpool_.set_no_zero_null();
  369. }
  370. // Record the target info from an input file.
  371. void
  372. record_target_info(const char* name, int machine, int size, bool big_endian,
  373. int osabi, int abiversion);
  374. // Add a string to the debug strings section.
  375. section_offset_type
  376. add_string(const char* str, size_t len);
  377. // Add a section to the output file, and return the new section offset.
  378. section_offset_type
  379. add_contribution(elfcpp::DW_SECT section_id, const unsigned char* contents,
  380. section_size_type len, int align);
  381. // Add a set of .debug_info and related sections to the output file.
  382. void
  383. add_cu_set(Unit_set* cu_set);
  384. // Lookup a type signature and return TRUE if we have already seen it.
  385. bool
  386. lookup_tu(uint64_t type_sig);
  387. // Add a set of .debug_types and related sections to the output file.
  388. void
  389. add_tu_set(Unit_set* tu_set);
  390. // Finalize the file, write the string tables and index sections,
  391. // and close the file.
  392. void
  393. finalize();
  394. private:
  395. // Contributions to output sections.
  396. struct Contribution
  397. {
  398. section_offset_type output_offset;
  399. section_size_type size;
  400. const unsigned char* contents;
  401. };
  402. // Sections in the output file.
  403. struct Section
  404. {
  405. const char* name;
  406. off_t offset;
  407. section_size_type size;
  408. int align;
  409. std::vector<Contribution> contributions;
  410. Section(const char* n, int a)
  411. : name(n), offset(0), size(0), align(a), contributions()
  412. { }
  413. };
  414. // The index sections defined by the DWARF Package File Format spec.
  415. class Dwp_index
  416. {
  417. public:
  418. // Vector for the section table.
  419. typedef std::vector<const Unit_set*> Section_table;
  420. Dwp_index()
  421. : capacity_(0), used_(0), hash_table_(NULL), section_table_(),
  422. section_mask_(0)
  423. { }
  424. ~Dwp_index()
  425. { }
  426. // Find a slot in the hash table for SIGNATURE. Return TRUE
  427. // if the entry already exists.
  428. bool
  429. find_or_add(uint64_t signature, unsigned int* slotp);
  430. // Enter a CU or TU set at the given SLOT in the hash table.
  431. void
  432. enter_set(unsigned int slot, const Unit_set* set);
  433. // Return the contents of the given SLOT in the hash table of signatures.
  434. uint64_t
  435. hash_table(unsigned int slot) const
  436. { return this->hash_table_[slot]; }
  437. // Return the contents of the given SLOT in the parallel table of
  438. // shndx pool indexes.
  439. uint32_t
  440. index_table(unsigned int slot) const
  441. { return this->index_table_[slot]; }
  442. // Return the total number of slots in the hash table.
  443. unsigned int
  444. hash_table_total_slots() const
  445. { return this->capacity_; }
  446. // Return the number of used slots in the hash table.
  447. unsigned int
  448. hash_table_used_slots() const
  449. { return this->used_; }
  450. // Return an iterator into the shndx pool.
  451. Section_table::const_iterator
  452. section_table() const
  453. { return this->section_table_.begin(); }
  454. Section_table::const_iterator
  455. section_table_end() const
  456. { return this->section_table_.end(); }
  457. // Return the number of rows in the section table.
  458. unsigned int
  459. section_table_rows() const
  460. { return this->section_table_.size(); }
  461. // Return the mask indicating which columns will be used
  462. // in the section table.
  463. int
  464. section_table_cols() const
  465. { return this->section_mask_; }
  466. private:
  467. // Initialize the hash table.
  468. void
  469. initialize();
  470. // Grow the hash table when we reach 2/3 capacity.
  471. void
  472. grow();
  473. // The number of slots in the table, a power of 2 such that
  474. // capacity > 3 * size / 2.
  475. unsigned int capacity_;
  476. // The current number of used slots in the hash table.
  477. unsigned int used_;
  478. // The storage for the hash table of signatures.
  479. uint64_t* hash_table_;
  480. // The storage for the parallel table of shndx pool indexes.
  481. uint32_t* index_table_;
  482. // The table of section offsets and sizes.
  483. Section_table section_table_;
  484. // Bit mask to indicate which debug sections are present in the file.
  485. int section_mask_;
  486. }; // End class Dwp_output_file::Dwp_index.
  487. // Add a new output section and return the section index.
  488. unsigned int
  489. add_output_section(const char* section_name, int align);
  490. // Write a new section to the output file.
  491. void
  492. write_new_section(const char* section_name, const unsigned char* contents,
  493. section_size_type len, int align);
  494. // Write the ELF header.
  495. void
  496. write_ehdr();
  497. template<unsigned int size, bool big_endian>
  498. void
  499. sized_write_ehdr();
  500. // Write a section header.
  501. void
  502. write_shdr(const char* name, unsigned int type, unsigned int flags,
  503. uint64_t addr, off_t offset, section_size_type sect_size,
  504. unsigned int link, unsigned int info,
  505. unsigned int align, unsigned int ent_size);
  506. template<unsigned int size, bool big_endian>
  507. void
  508. sized_write_shdr(const char* name, unsigned int type, unsigned int flags,
  509. uint64_t addr, off_t offset, section_size_type sect_size,
  510. unsigned int link, unsigned int info,
  511. unsigned int align, unsigned int ent_size);
  512. // Write the contributions to an output section.
  513. void
  514. write_contributions(const Section& sect);
  515. // Write a CU or TU index section.
  516. template<bool big_endian>
  517. void
  518. write_index(const char* sect_name, const Dwp_index& index);
  519. // The output filename.
  520. const char* name_;
  521. // ELF header parameters.
  522. int machine_;
  523. int size_;
  524. int big_endian_;
  525. int osabi_;
  526. int abiversion_;
  527. // The output file descriptor.
  528. FILE* fd_;
  529. // Next available file offset.
  530. off_t next_file_offset_;
  531. // The number of sections.
  532. unsigned int shnum_;
  533. // Section table. The first entry is shndx 1.
  534. std::vector<Section> sections_;
  535. // Section id map. This maps a DW_SECT enum to an shndx.
  536. std::vector<unsigned int> section_id_map_;
  537. // File offset of the section header table.
  538. off_t shoff_;
  539. // Section index of the section string table.
  540. unsigned int shstrndx_;
  541. // TRUE if we have added any strings to the string pool.
  542. bool have_strings_;
  543. // String pool for the output .debug_str.dwo section.
  544. Stringpool stringpool_;
  545. // String pool for the .shstrtab section.
  546. Stringpool shstrtab_;
  547. // The compilation unit index.
  548. Dwp_index cu_index_;
  549. // The type unit index.
  550. Dwp_index tu_index_;
  551. // Cache of the last type signature looked up.
  552. uint64_t last_type_sig_;
  553. // Cache of the slot index for the last type signature.
  554. unsigned int last_tu_slot_;
  555. };
  556. // A specialization of Dwarf_info_reader, for reading dwo_names from
  557. // DWARF CUs.
  558. class Dwo_name_info_reader : public Dwarf_info_reader
  559. {
  560. public:
  561. Dwo_name_info_reader(Relobj* object, unsigned int shndx)
  562. : Dwarf_info_reader(false, object, NULL, 0, shndx, 0, 0),
  563. files_(NULL)
  564. { }
  565. ~Dwo_name_info_reader()
  566. { }
  567. // Get the dwo_names from the DWARF compilation unit DIEs.
  568. void
  569. get_dwo_names(File_list* files)
  570. {
  571. this->files_ = files;
  572. this->parse();
  573. }
  574. protected:
  575. // Visit a compilation unit.
  576. virtual void
  577. visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
  578. private:
  579. // The list of files to populate.
  580. File_list* files_;
  581. };
  582. // A specialization of Dwarf_info_reader, for reading DWARF CUs and TUs
  583. // and adding them to the output file.
  584. class Unit_reader : public Dwarf_info_reader
  585. {
  586. public:
  587. Unit_reader(bool is_type_unit, Relobj* object, unsigned int shndx)
  588. : Dwarf_info_reader(is_type_unit, object, NULL, 0, shndx, 0, 0),
  589. output_file_(NULL), sections_(NULL)
  590. { }
  591. ~Unit_reader()
  592. { }
  593. // Read the CUs or TUs and add them to the output file.
  594. void
  595. add_units(Dwp_output_file*, unsigned int debug_abbrev, Section_bounds*);
  596. protected:
  597. // Visit a compilation unit.
  598. virtual void
  599. visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
  600. // Visit a type unit.
  601. virtual void
  602. visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
  603. uint64_t signature, Dwarf_die*);
  604. private:
  605. Dwp_output_file* output_file_;
  606. Section_bounds* sections_;
  607. };
  608. // Return the name of a DWARF .dwo section.
  609. static const char*
  610. get_dwarf_section_name(elfcpp::DW_SECT section_id)
  611. {
  612. static const char* dwarf_section_names[] = {
  613. NULL, // unused
  614. ".debug_info.dwo", // DW_SECT_INFO = 1
  615. ".debug_types.dwo", // DW_SECT_TYPES = 2
  616. ".debug_abbrev.dwo", // DW_SECT_ABBREV = 3
  617. ".debug_line.dwo", // DW_SECT_LINE = 4
  618. ".debug_loc.dwo", // DW_SECT_LOC = 5
  619. ".debug_str_offsets.dwo", // DW_SECT_STR_OFFSETS = 6
  620. ".debug_macinfo.dwo", // DW_SECT_MACINFO = 7
  621. ".debug_macro.dwo", // DW_SECT_MACRO = 8
  622. };
  623. gold_assert(section_id > 0 && section_id <= elfcpp::DW_SECT_MAX);
  624. return dwarf_section_names[section_id];
  625. }
  626. // Class Sized_relobj_dwo.
  627. // Setup the section information.
  628. template <int size, bool big_endian>
  629. void
  630. Sized_relobj_dwo<size, big_endian>::setup()
  631. {
  632. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  633. const off_t shoff = this->elf_file_.shoff();
  634. const unsigned int shnum = this->elf_file_.shnum();
  635. this->set_shnum(shnum);
  636. this->section_offsets().resize(shnum);
  637. // Read the section headers.
  638. const unsigned char* const pshdrs = this->get_view(shoff, shnum * shdr_size,
  639. true, false);
  640. // Read the section names.
  641. const unsigned char* pshdrnames =
  642. pshdrs + this->elf_file_.shstrndx() * shdr_size;
  643. typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
  644. if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
  645. this->error(_("section name section has wrong type: %u"),
  646. static_cast<unsigned int>(shdrnames.get_sh_type()));
  647. section_size_type section_names_size =
  648. convert_to_section_size_type(shdrnames.get_sh_size());
  649. const unsigned char* namesu = this->get_view(shdrnames.get_sh_offset(),
  650. section_names_size, false,
  651. false);
  652. const char* names = reinterpret_cast<const char*>(namesu);
  653. Compressed_section_map* compressed_sections =
  654. build_compressed_section_map<size, big_endian>(
  655. pshdrs, this->shnum(), names, section_names_size, this, true);
  656. if (compressed_sections != NULL && !compressed_sections->empty())
  657. this->set_compressed_sections(compressed_sections);
  658. }
  659. // Return a view of the contents of a section.
  660. template <int size, bool big_endian>
  661. const unsigned char*
  662. Sized_relobj_dwo<size, big_endian>::do_section_contents(
  663. unsigned int shndx,
  664. section_size_type* plen,
  665. bool cache)
  666. {
  667. Object::Location loc(this->elf_file_.section_contents(shndx));
  668. *plen = convert_to_section_size_type(loc.data_size);
  669. if (*plen == 0)
  670. {
  671. static const unsigned char empty[1] = { '\0' };
  672. return empty;
  673. }
  674. return this->get_view(loc.file_offset, *plen, true, cache);
  675. }
  676. // Class Dwo_file.
  677. Dwo_file::~Dwo_file()
  678. {
  679. if (this->obj_ != NULL)
  680. delete this->obj_;
  681. if (this->input_file_ != NULL)
  682. delete this->input_file_;
  683. }
  684. // Read the input executable file and extract the list of .dwo files
  685. // that it references.
  686. void
  687. Dwo_file::read_executable(File_list* files)
  688. {
  689. this->obj_ = this->make_object(NULL);
  690. unsigned int shnum = this->shnum();
  691. this->is_compressed_.resize(shnum);
  692. this->sect_offsets_.resize(shnum);
  693. unsigned int debug_info = 0;
  694. unsigned int debug_abbrev = 0;
  695. // Scan the section table and collect the debug sections we need.
  696. // (Section index 0 is a dummy section; skip it.)
  697. for (unsigned int i = 1; i < shnum; i++)
  698. {
  699. if (this->section_type(i) != elfcpp::SHT_PROGBITS)
  700. continue;
  701. std::string sect_name = this->section_name(i);
  702. const char* suffix = sect_name.c_str();
  703. if (is_prefix_of(".debug_", suffix))
  704. suffix += 7;
  705. else if (is_prefix_of(".zdebug_", suffix))
  706. {
  707. this->is_compressed_[i] = true;
  708. suffix += 8;
  709. }
  710. else
  711. continue;
  712. if (strcmp(suffix, "info") == 0)
  713. debug_info = i;
  714. else if (strcmp(suffix, "abbrev") == 0)
  715. debug_abbrev = i;
  716. }
  717. if (debug_info > 0)
  718. {
  719. Dwo_name_info_reader dwarf_reader(this->obj_, debug_info);
  720. dwarf_reader.set_abbrev_shndx(debug_abbrev);
  721. dwarf_reader.get_dwo_names(files);
  722. }
  723. }
  724. // Read the input file and send its contents to OUTPUT_FILE.
  725. void
  726. Dwo_file::read(Dwp_output_file* output_file)
  727. {
  728. this->obj_ = this->make_object(output_file);
  729. unsigned int shnum = this->shnum();
  730. this->is_compressed_.resize(shnum);
  731. this->sect_offsets_.resize(shnum);
  732. typedef std::vector<unsigned int> Types_list;
  733. Types_list debug_types;
  734. unsigned int debug_shndx[elfcpp::DW_SECT_MAX + 1];
  735. for (unsigned int i = 0; i <= elfcpp::DW_SECT_MAX; i++)
  736. debug_shndx[i] = 0;
  737. unsigned int debug_str = 0;
  738. unsigned int debug_cu_index = 0;
  739. unsigned int debug_tu_index = 0;
  740. // Scan the section table and collect debug sections.
  741. // (Section index 0 is a dummy section; skip it.)
  742. for (unsigned int i = 1; i < shnum; i++)
  743. {
  744. if (this->section_type(i) != elfcpp::SHT_PROGBITS)
  745. continue;
  746. std::string sect_name = this->section_name(i);
  747. const char* suffix = sect_name.c_str();
  748. if (is_prefix_of(".debug_", suffix))
  749. suffix += 7;
  750. else if (is_prefix_of(".zdebug_", suffix))
  751. {
  752. this->is_compressed_[i] = true;
  753. suffix += 8;
  754. }
  755. else
  756. continue;
  757. if (strcmp(suffix, "info.dwo") == 0)
  758. debug_shndx[elfcpp::DW_SECT_INFO] = i;
  759. else if (strcmp(suffix, "types.dwo") == 0)
  760. debug_types.push_back(i);
  761. else if (strcmp(suffix, "abbrev.dwo") == 0)
  762. debug_shndx[elfcpp::DW_SECT_ABBREV] = i;
  763. else if (strcmp(suffix, "line.dwo") == 0)
  764. debug_shndx[elfcpp::DW_SECT_LINE] = i;
  765. else if (strcmp(suffix, "loc.dwo") == 0)
  766. debug_shndx[elfcpp::DW_SECT_LOC] = i;
  767. else if (strcmp(suffix, "str.dwo") == 0)
  768. debug_str = i;
  769. else if (strcmp(suffix, "str_offsets.dwo") == 0)
  770. debug_shndx[elfcpp::DW_SECT_STR_OFFSETS] = i;
  771. else if (strcmp(suffix, "macinfo.dwo") == 0)
  772. debug_shndx[elfcpp::DW_SECT_MACINFO] = i;
  773. else if (strcmp(suffix, "macro.dwo") == 0)
  774. debug_shndx[elfcpp::DW_SECT_MACRO] = i;
  775. else if (strcmp(suffix, "cu_index") == 0)
  776. debug_cu_index = i;
  777. else if (strcmp(suffix, "tu_index") == 0)
  778. debug_tu_index = i;
  779. }
  780. // Merge the input string table into the output string table.
  781. this->add_strings(output_file, debug_str);
  782. // If we found any .dwp index sections, read those and add the section
  783. // sets to the output file.
  784. if (debug_cu_index > 0 || debug_tu_index > 0)
  785. {
  786. if (debug_cu_index > 0)
  787. this->read_unit_index(debug_cu_index, debug_shndx, output_file, false);
  788. if (debug_tu_index > 0)
  789. {
  790. if (debug_types.size() > 1)
  791. gold_fatal(_("%s: .dwp file must have no more than one "
  792. ".debug_types.dwo section"), this->name_);
  793. if (debug_types.size() == 1)
  794. debug_shndx[elfcpp::DW_SECT_TYPES] = debug_types[0];
  795. else
  796. debug_shndx[elfcpp::DW_SECT_TYPES] = 0;
  797. this->read_unit_index(debug_tu_index, debug_shndx, output_file, true);
  798. }
  799. return;
  800. }
  801. // If we found no index sections, this is a .dwo file.
  802. if (debug_shndx[elfcpp::DW_SECT_INFO] > 0)
  803. this->add_unit_set(output_file, debug_shndx, false);
  804. debug_shndx[elfcpp::DW_SECT_INFO] = 0;
  805. for (Types_list::const_iterator tp = debug_types.begin();
  806. tp != debug_types.end();
  807. ++tp)
  808. {
  809. debug_shndx[elfcpp::DW_SECT_TYPES] = *tp;
  810. this->add_unit_set(output_file, debug_shndx, true);
  811. }
  812. }
  813. // Verify a .dwp file given a list of .dwo files referenced by the
  814. // corresponding executable file. Returns true if no problems
  815. // were found.
  816. bool
  817. Dwo_file::verify(const File_list& files)
  818. {
  819. this->obj_ = this->make_object(NULL);
  820. unsigned int shnum = this->shnum();
  821. this->is_compressed_.resize(shnum);
  822. this->sect_offsets_.resize(shnum);
  823. unsigned int debug_cu_index = 0;
  824. // Scan the section table and collect debug sections.
  825. // (Section index 0 is a dummy section; skip it.)
  826. for (unsigned int i = 1; i < shnum; i++)
  827. {
  828. if (this->section_type(i) != elfcpp::SHT_PROGBITS)
  829. continue;
  830. std::string sect_name = this->section_name(i);
  831. const char* suffix = sect_name.c_str();
  832. if (is_prefix_of(".debug_", suffix))
  833. suffix += 7;
  834. else if (is_prefix_of(".zdebug_", suffix))
  835. {
  836. this->is_compressed_[i] = true;
  837. suffix += 8;
  838. }
  839. else
  840. continue;
  841. if (strcmp(suffix, "cu_index") == 0)
  842. debug_cu_index = i;
  843. }
  844. if (debug_cu_index == 0)
  845. gold_fatal(_("%s: no .debug_cu_index section found"), this->name_);
  846. return this->verify_dwo_list(debug_cu_index, files);
  847. }
  848. // Create a Sized_relobj_dwo of the given size and endianness,
  849. // and record the target info.
  850. Relobj*
  851. Dwo_file::make_object(Dwp_output_file* output_file)
  852. {
  853. // Open the input file.
  854. Input_file* input_file = new Input_file(this->name_);
  855. this->input_file_ = input_file;
  856. Dirsearch dirpath;
  857. int index;
  858. if (!input_file->open(dirpath, NULL, &index))
  859. gold_fatal(_("%s: can't open"), this->name_);
  860. // Check that it's an ELF file.
  861. off_t filesize = input_file->file().filesize();
  862. int hdrsize = elfcpp::Elf_recognizer::max_header_size;
  863. if (filesize < hdrsize)
  864. hdrsize = filesize;
  865. const unsigned char* elf_header =
  866. input_file->file().get_view(0, 0, hdrsize, true, false);
  867. if (!elfcpp::Elf_recognizer::is_elf_file(elf_header, hdrsize))
  868. gold_fatal(_("%s: not an ELF object file"), this->name_);
  869. // Get the size, endianness, machine, etc. info from the header,
  870. // make an appropriately-sized Relobj, and pass the target info
  871. // to the output object.
  872. int size;
  873. bool big_endian;
  874. std::string error;
  875. if (!elfcpp::Elf_recognizer::is_valid_header(elf_header, hdrsize, &size,
  876. &big_endian, &error))
  877. gold_fatal(_("%s: %s"), this->name_, error.c_str());
  878. if (size == 32)
  879. {
  880. if (big_endian)
  881. #ifdef HAVE_TARGET_32_BIG
  882. return this->sized_make_object<32, true>(elf_header, input_file,
  883. output_file);
  884. #else
  885. gold_unreachable();
  886. #endif
  887. else
  888. #ifdef HAVE_TARGET_32_LITTLE
  889. return this->sized_make_object<32, false>(elf_header, input_file,
  890. output_file);
  891. #else
  892. gold_unreachable();
  893. #endif
  894. }
  895. else if (size == 64)
  896. {
  897. if (big_endian)
  898. #ifdef HAVE_TARGET_64_BIG
  899. return this->sized_make_object<64, true>(elf_header, input_file,
  900. output_file);
  901. #else
  902. gold_unreachable();
  903. #endif
  904. else
  905. #ifdef HAVE_TARGET_64_LITTLE
  906. return this->sized_make_object<64, false>(elf_header, input_file,
  907. output_file);
  908. #else
  909. gold_unreachable();
  910. #endif
  911. }
  912. else
  913. gold_unreachable();
  914. }
  915. // Function template to create a Sized_relobj_dwo and record the target info.
  916. // P is a pointer to the ELF header in memory.
  917. template <int size, bool big_endian>
  918. Relobj*
  919. Dwo_file::sized_make_object(const unsigned char* p, Input_file* input_file,
  920. Dwp_output_file* output_file)
  921. {
  922. elfcpp::Ehdr<size, big_endian> ehdr(p);
  923. Sized_relobj_dwo<size, big_endian>* obj =
  924. new Sized_relobj_dwo<size, big_endian>(this->name_, input_file, ehdr);
  925. obj->setup();
  926. if (output_file != NULL)
  927. output_file->record_target_info(
  928. this->name_, ehdr.get_e_machine(), size, big_endian,
  929. ehdr.get_ei_osabi(),
  930. ehdr.get_ei_abiversion());
  931. return obj;
  932. }
  933. // Read the .debug_cu_index or .debug_tu_index section of a .dwp file,
  934. // and process the CU or TU sets.
  935. void
  936. Dwo_file::read_unit_index(unsigned int shndx, unsigned int *debug_shndx,
  937. Dwp_output_file* output_file, bool is_tu_index)
  938. {
  939. if (this->obj_->is_big_endian())
  940. this->sized_read_unit_index<true>(shndx, debug_shndx, output_file,
  941. is_tu_index);
  942. else
  943. this->sized_read_unit_index<false>(shndx, debug_shndx, output_file,
  944. is_tu_index);
  945. }
  946. template <bool big_endian>
  947. void
  948. Dwo_file::sized_read_unit_index(unsigned int shndx,
  949. unsigned int *debug_shndx,
  950. Dwp_output_file* output_file,
  951. bool is_tu_index)
  952. {
  953. elfcpp::DW_SECT info_sect = (is_tu_index
  954. ? elfcpp::DW_SECT_TYPES
  955. : elfcpp::DW_SECT_INFO);
  956. unsigned int info_shndx = debug_shndx[info_sect];
  957. gold_assert(shndx > 0);
  958. section_size_type index_len;
  959. bool index_is_new;
  960. const unsigned char* contents =
  961. this->section_contents(shndx, &index_len, &index_is_new);
  962. unsigned int version =
  963. elfcpp::Swap_unaligned<32, big_endian>::readval(contents);
  964. // We don't support version 1 anymore because it was experimental
  965. // and because in normal use, dwp is not expected to read .dwp files
  966. // produced by an earlier version of the tool.
  967. if (version != 2)
  968. gold_fatal(_("%s: section %s has unsupported version number %d"),
  969. this->name_, this->section_name(shndx).c_str(), version);
  970. unsigned int ncols =
  971. elfcpp::Swap_unaligned<32, big_endian>::readval(contents
  972. + sizeof(uint32_t));
  973. unsigned int nused =
  974. elfcpp::Swap_unaligned<32, big_endian>::readval(contents
  975. + 2 * sizeof(uint32_t));
  976. if (ncols == 0 || nused == 0)
  977. return;
  978. gold_assert(info_shndx > 0);
  979. unsigned int nslots =
  980. elfcpp::Swap_unaligned<32, big_endian>::readval(contents
  981. + 3 * sizeof(uint32_t));
  982. const unsigned char* phash = contents + 4 * sizeof(uint32_t);
  983. const unsigned char* pindex = phash + nslots * sizeof(uint64_t);
  984. const unsigned char* pcolhdrs = pindex + nslots * sizeof(uint32_t);
  985. const unsigned char* poffsets = pcolhdrs + ncols * sizeof(uint32_t);
  986. const unsigned char* psizes = poffsets + nused * ncols * sizeof(uint32_t);
  987. const unsigned char* pend = psizes + nused * ncols * sizeof(uint32_t);
  988. if (pend > contents + index_len)
  989. gold_fatal(_("%s: section %s is corrupt"), this->name_,
  990. this->section_name(shndx).c_str());
  991. // Copy the related sections and track the section offsets and sizes.
  992. Section_bounds sections[elfcpp::DW_SECT_MAX + 1];
  993. for (int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i)
  994. {
  995. if (debug_shndx[i] > 0)
  996. sections[i] = this->copy_section(output_file, debug_shndx[i],
  997. static_cast<elfcpp::DW_SECT>(i));
  998. }
  999. // Get the contents of the .debug_info.dwo or .debug_types.dwo section.
  1000. section_size_type info_len;
  1001. bool info_is_new;
  1002. const unsigned char* info_contents =
  1003. this->section_contents(info_shndx, &info_len, &info_is_new);
  1004. // Loop over the slots of the hash table.
  1005. for (unsigned int i = 0; i < nslots; ++i)
  1006. {
  1007. uint64_t signature =
  1008. elfcpp::Swap_unaligned<64, big_endian>::readval(phash);
  1009. unsigned int index =
  1010. elfcpp::Swap_unaligned<32, big_endian>::readval(pindex);
  1011. if (index != 0 && (!is_tu_index || !output_file->lookup_tu(signature)))
  1012. {
  1013. Unit_set* unit_set = new Unit_set();
  1014. unit_set->signature = signature;
  1015. const unsigned char* pch = pcolhdrs;
  1016. const unsigned char* porow =
  1017. poffsets + (index - 1) * ncols * sizeof(uint32_t);
  1018. const unsigned char* psrow =
  1019. psizes + (index - 1) * ncols * sizeof(uint32_t);
  1020. // Adjust the offset of each contribution within the input section
  1021. // by the offset of the input section within the output section.
  1022. for (unsigned int j = 0; j <= ncols; j++)
  1023. {
  1024. unsigned int dw_sect =
  1025. elfcpp::Swap_unaligned<64, big_endian>::readval(pch);
  1026. unsigned int offset =
  1027. elfcpp::Swap_unaligned<64, big_endian>::readval(porow);
  1028. unsigned int size =
  1029. elfcpp::Swap_unaligned<64, big_endian>::readval(psrow);
  1030. unit_set->sections[dw_sect].offset = (sections[dw_sect].offset
  1031. + offset);
  1032. unit_set->sections[dw_sect].size = size;
  1033. pch += sizeof(uint32_t);
  1034. porow += sizeof(uint32_t);
  1035. psrow += sizeof(uint32_t);
  1036. }
  1037. const unsigned char* unit_start =
  1038. info_contents + unit_set->sections[info_sect].offset;
  1039. section_size_type unit_length = unit_set->sections[info_sect].size;
  1040. // Dwp_output_file::add_contribution writes the .debug_info.dwo
  1041. // section directly to the output file, so we only need to
  1042. // duplicate contributions for .debug_types.dwo section.
  1043. if (is_tu_index)
  1044. {
  1045. unsigned char *copy = new unsigned char[unit_length];
  1046. memcpy(copy, unit_start, unit_length);
  1047. unit_start = copy;
  1048. }
  1049. section_offset_type off =
  1050. output_file->add_contribution(info_sect, unit_start,
  1051. unit_length, 1);
  1052. unit_set->sections[info_sect].offset = off;
  1053. if (is_tu_index)
  1054. output_file->add_tu_set(unit_set);
  1055. else
  1056. output_file->add_cu_set(unit_set);
  1057. }
  1058. phash += sizeof(uint64_t);
  1059. pindex += sizeof(uint32_t);
  1060. }
  1061. if (index_is_new)
  1062. delete[] contents;
  1063. if (info_is_new)
  1064. delete[] info_contents;
  1065. }
  1066. // Verify the .debug_cu_index section of a .dwp file, comparing it
  1067. // against the list of .dwo files referenced by the corresponding
  1068. // executable file.
  1069. bool
  1070. Dwo_file::verify_dwo_list(unsigned int shndx, const File_list& files)
  1071. {
  1072. if (this->obj_->is_big_endian())
  1073. return this->sized_verify_dwo_list<true>(shndx, files);
  1074. else
  1075. return this->sized_verify_dwo_list<false>(shndx, files);
  1076. }
  1077. template <bool big_endian>
  1078. bool
  1079. Dwo_file::sized_verify_dwo_list(unsigned int shndx, const File_list& files)
  1080. {
  1081. gold_assert(shndx > 0);
  1082. section_size_type index_len;
  1083. bool index_is_new;
  1084. const unsigned char* contents =
  1085. this->section_contents(shndx, &index_len, &index_is_new);
  1086. unsigned int version =
  1087. elfcpp::Swap_unaligned<32, big_endian>::readval(contents);
  1088. // We don't support version 1 anymore because it was experimental
  1089. // and because in normal use, dwp is not expected to read .dwp files
  1090. // produced by an earlier version of the tool.
  1091. if (version != 2)
  1092. gold_fatal(_("%s: section %s has unsupported version number %d"),
  1093. this->name_, this->section_name(shndx).c_str(), version);
  1094. unsigned int ncols =
  1095. elfcpp::Swap_unaligned<32, big_endian>::readval(contents
  1096. + sizeof(uint32_t));
  1097. unsigned int nused =
  1098. elfcpp::Swap_unaligned<32, big_endian>::readval(contents
  1099. + 2 * sizeof(uint32_t));
  1100. if (ncols == 0 || nused == 0)
  1101. return true;
  1102. unsigned int nslots =
  1103. elfcpp::Swap_unaligned<32, big_endian>::readval(contents
  1104. + 3 * sizeof(uint32_t));
  1105. const unsigned char* phash = contents + 4 * sizeof(uint32_t);
  1106. const unsigned char* pindex = phash + nslots * sizeof(uint64_t);
  1107. const unsigned char* pcolhdrs = pindex + nslots * sizeof(uint32_t);
  1108. const unsigned char* poffsets = pcolhdrs + ncols * sizeof(uint32_t);
  1109. const unsigned char* psizes = poffsets + nused * ncols * sizeof(uint32_t);
  1110. const unsigned char* pend = psizes + nused * ncols * sizeof(uint32_t);
  1111. if (pend > contents + index_len)
  1112. gold_fatal(_("%s: section %s is corrupt"), this->name_,
  1113. this->section_name(shndx).c_str());
  1114. int nmissing = 0;
  1115. for (File_list::const_iterator f = files.begin(); f != files.end(); ++f)
  1116. {
  1117. uint64_t dwo_id = f->dwo_id;
  1118. unsigned int slot = static_cast<unsigned int>(dwo_id) & (nslots - 1);
  1119. const unsigned char* ph = phash + slot * sizeof(uint64_t);
  1120. const unsigned char* pi = pindex + slot * sizeof(uint32_t);
  1121. uint64_t probe = elfcpp::Swap_unaligned<64, big_endian>::readval(ph);
  1122. uint32_t row_index = elfcpp::Swap_unaligned<32, big_endian>::readval(pi);
  1123. if (row_index != 0 && probe != dwo_id)
  1124. {
  1125. unsigned int h2 = ((static_cast<unsigned int>(dwo_id >> 32)
  1126. & (nslots - 1)) | 1);
  1127. do
  1128. {
  1129. slot = (slot + h2) & (nslots - 1);
  1130. ph = phash + slot * sizeof(uint64_t);
  1131. pi = pindex + slot * sizeof(uint32_t);
  1132. probe = elfcpp::Swap_unaligned<64, big_endian>::readval(ph);
  1133. row_index = elfcpp::Swap_unaligned<32, big_endian>::readval(pi);
  1134. } while (row_index != 0 && probe != dwo_id);
  1135. }
  1136. if (row_index == 0)
  1137. {
  1138. printf(_("missing .dwo file: %016llx %s\n"),
  1139. static_cast<long long>(dwo_id), f->dwo_name.c_str());
  1140. ++nmissing;
  1141. }
  1142. }
  1143. gold_info(_("Found %d missing .dwo files"), nmissing);
  1144. if (index_is_new)
  1145. delete[] contents;
  1146. return nmissing == 0;
  1147. }
  1148. // Merge the input string table section into the output file.
  1149. void
  1150. Dwo_file::add_strings(Dwp_output_file* output_file, unsigned int debug_str)
  1151. {
  1152. section_size_type len;
  1153. bool is_new;
  1154. const unsigned char* pdata = this->section_contents(debug_str, &len, &is_new);
  1155. const char* p = reinterpret_cast<const char*>(pdata);
  1156. const char* pend = p + len;
  1157. // Check that the last string is null terminated.
  1158. if (pend[-1] != '\0')
  1159. gold_fatal(_("%s: last entry in string section '%s' "
  1160. "is not null terminated"),
  1161. this->name_,
  1162. this->section_name(debug_str).c_str());
  1163. // Count the number of strings in the section, and size the map.
  1164. size_t count = 0;
  1165. for (const char* pt = p; pt < pend; pt += strlen(pt) + 1)
  1166. ++count;
  1167. this->str_offset_map_.reserve(count + 1);
  1168. // Add the strings to the output string table, and record the new offsets
  1169. // in the map.
  1170. section_offset_type i = 0;
  1171. section_offset_type new_offset;
  1172. while (p < pend)
  1173. {
  1174. size_t len = strlen(p);
  1175. new_offset = output_file->add_string(p, len);
  1176. this->str_offset_map_.push_back(std::make_pair(i, new_offset));
  1177. p += len + 1;
  1178. i += len + 1;
  1179. }
  1180. new_offset = 0;
  1181. this->str_offset_map_.push_back(std::make_pair(i, new_offset));
  1182. if (is_new)
  1183. delete[] pdata;
  1184. }
  1185. // Copy a section from the input file to the output file.
  1186. // Return the offset and length of this input section's contribution
  1187. // in the output section. If copying .debug_str_offsets.dwo, remap
  1188. // the string offsets for the output string table.
  1189. Section_bounds
  1190. Dwo_file::copy_section(Dwp_output_file* output_file, unsigned int shndx,
  1191. elfcpp::DW_SECT section_id)
  1192. {
  1193. // Some sections may be referenced from more than one set.
  1194. // Don't copy a section more than once.
  1195. if (this->sect_offsets_[shndx].size > 0)
  1196. return this->sect_offsets_[shndx];
  1197. // Get the section contents. Upon return, if IS_NEW is true, the memory
  1198. // has been allocated via new; if false, the memory is part of the mapped
  1199. // input file, and we will need to duplicate it so that it will persist
  1200. // after we close the input file.
  1201. section_size_type len;
  1202. bool is_new;
  1203. const unsigned char* contents = this->section_contents(shndx, &len, &is_new);
  1204. if (section_id == elfcpp::DW_SECT_STR_OFFSETS)
  1205. {
  1206. const unsigned char* remapped = this->remap_str_offsets(contents, len);
  1207. if (is_new)
  1208. delete[] contents;
  1209. contents = remapped;
  1210. }
  1211. else if (!is_new)
  1212. {
  1213. unsigned char* copy = new unsigned char[len];
  1214. memcpy(copy, contents, len);
  1215. contents = copy;
  1216. }
  1217. // Add the contents of the input section to the output section.
  1218. // The output file takes ownership of the memory pointed to by CONTENTS.
  1219. section_offset_type off = output_file->add_contribution(section_id, contents,
  1220. len, 1);
  1221. // Store the output section bounds.
  1222. Section_bounds bounds(off, len);
  1223. this->sect_offsets_[shndx] = bounds;
  1224. return bounds;
  1225. }
  1226. // Remap the
  1227. const unsigned char*
  1228. Dwo_file::remap_str_offsets(const unsigned char* contents,
  1229. section_size_type len)
  1230. {
  1231. if ((len & 3) != 0)
  1232. gold_fatal(_("%s: .debug_str_offsets.dwo section size not a multiple of 4"),
  1233. this->name_);
  1234. if (this->obj_->is_big_endian())
  1235. return this->sized_remap_str_offsets<true>(contents, len);
  1236. else
  1237. return this->sized_remap_str_offsets<false>(contents, len);
  1238. }
  1239. template <bool big_endian>
  1240. const unsigned char*
  1241. Dwo_file::sized_remap_str_offsets(const unsigned char* contents,
  1242. section_size_type len)
  1243. {
  1244. unsigned char* remapped = new unsigned char[len];
  1245. const unsigned char* p = contents;
  1246. unsigned char* q = remapped;
  1247. while (len > 0)
  1248. {
  1249. unsigned int val = elfcpp::Swap_unaligned<32, big_endian>::readval(p);
  1250. val = this->remap_str_offset(val);
  1251. elfcpp::Swap_unaligned<32, big_endian>::writeval(q, val);
  1252. len -= 4;
  1253. p += 4;
  1254. q += 4;
  1255. }
  1256. return remapped;
  1257. }
  1258. unsigned int
  1259. Dwo_file::remap_str_offset(section_offset_type val)
  1260. {
  1261. Str_offset_map_entry entry;
  1262. entry.first = val;
  1263. Str_offset_map::const_iterator p =
  1264. std::lower_bound(this->str_offset_map_.begin(),
  1265. this->str_offset_map_.end(),
  1266. entry, Offset_compare());
  1267. if (p == this->str_offset_map_.end() || p->first > val)
  1268. {
  1269. if (p == this->str_offset_map_.begin())
  1270. return 0;
  1271. --p;
  1272. gold_assert(p->first <= val);
  1273. }
  1274. return p->second + (val - p->first);
  1275. }
  1276. // Add a set of .debug_info.dwo or .debug_types.dwo and related sections
  1277. // to OUTPUT_FILE.
  1278. void
  1279. Dwo_file::add_unit_set(Dwp_output_file* output_file, unsigned int *debug_shndx,
  1280. bool is_debug_types)
  1281. {
  1282. unsigned int shndx = (is_debug_types
  1283. ? debug_shndx[elfcpp::DW_SECT_TYPES]
  1284. : debug_shndx[elfcpp::DW_SECT_INFO]);
  1285. gold_assert(shndx != 0);
  1286. if (debug_shndx[elfcpp::DW_SECT_ABBREV] == 0)
  1287. gold_fatal(_("%s: no .debug_abbrev.dwo section found"), this->name_);
  1288. // Copy the related sections and track the section offsets and sizes.
  1289. Section_bounds sections[elfcpp::DW_SECT_MAX + 1];
  1290. for (int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i)
  1291. {
  1292. if (debug_shndx[i] > 0)
  1293. sections[i] = this->copy_section(output_file, debug_shndx[i],
  1294. static_cast<elfcpp::DW_SECT>(i));
  1295. }
  1296. // Parse the .debug_info or .debug_types section and add each compilation
  1297. // or type unit to the output file, along with the contributions to the
  1298. // related sections.
  1299. Unit_reader reader(is_debug_types, this->obj_, shndx);
  1300. reader.add_units(output_file, debug_shndx[elfcpp::DW_SECT_ABBREV], sections);
  1301. }
  1302. // Class Dwp_output_file.
  1303. // Record the target info from an input file. On first call, we
  1304. // set the ELF header values for the output file. On subsequent
  1305. // calls, we just verify that the values match.
  1306. void
  1307. Dwp_output_file::record_target_info(const char*, int machine,
  1308. int size, bool big_endian,
  1309. int osabi, int abiversion)
  1310. {
  1311. // TODO: Check the values on subsequent calls.
  1312. if (this->size_ > 0)
  1313. return;
  1314. this->machine_ = machine;
  1315. this->size_ = size;
  1316. this->big_endian_ = big_endian;
  1317. this->osabi_ = osabi;
  1318. this->abiversion_ = abiversion;
  1319. if (size == 32)
  1320. this->next_file_offset_ = elfcpp::Elf_sizes<32>::ehdr_size;
  1321. else if (size == 64)
  1322. this->next_file_offset_ = elfcpp::Elf_sizes<64>::ehdr_size;
  1323. else
  1324. gold_unreachable();
  1325. this->fd_ = ::fopen(this->name_, "wb");
  1326. if (this->fd_ == NULL)
  1327. gold_fatal(_("%s: %s"), this->name_, strerror(errno));
  1328. // Write zeroes for the ELF header initially. We'll write
  1329. // the actual header during finalize().
  1330. static const char buf[elfcpp::Elf_sizes<64>::ehdr_size] = { 0 };
  1331. if (::fwrite(buf, 1, this->next_file_offset_, this->fd_)
  1332. < (size_t) this->next_file_offset_)
  1333. gold_fatal(_("%s: %s"), this->name_, strerror(errno));
  1334. }
  1335. // Add a string to the debug strings section.
  1336. section_offset_type
  1337. Dwp_output_file::add_string(const char* str, size_t len)
  1338. {
  1339. Stringpool::Key key;
  1340. this->stringpool_.add_with_length(str, len, true, &key);
  1341. this->have_strings_ = true;
  1342. // We aren't supposed to call get_offset() until after
  1343. // calling set_string_offsets(), but the offsets will
  1344. // not change unless optimizing the string pool.
  1345. return this->stringpool_.get_offset_from_key(key);
  1346. }
  1347. // Align the file offset to the given boundary.
  1348. static inline off_t
  1349. align_offset(off_t off, int align)
  1350. {
  1351. return (off + align - 1) & ~(align - 1);
  1352. }
  1353. // Add a new output section and return the section index.
  1354. unsigned int
  1355. Dwp_output_file::add_output_section(const char* section_name, int align)
  1356. {
  1357. Section sect(section_name, align);
  1358. this->sections_.push_back(sect);
  1359. return this->shnum_++;
  1360. }
  1361. // Add a contribution to a section in the output file, and return the offset
  1362. // of the contribution within the output section. The .debug_info.dwo section
  1363. // is expected to be the largest one, so we will write the contents of this
  1364. // section directly to the output file as we receive contributions, allowing
  1365. // us to free that memory as soon as possible. We will save the remaining
  1366. // contributions until we finalize the layout of the output file.
  1367. section_offset_type
  1368. Dwp_output_file::add_contribution(elfcpp::DW_SECT section_id,
  1369. const unsigned char* contents,
  1370. section_size_type len,
  1371. int align)
  1372. {
  1373. const char* section_name = get_dwarf_section_name(section_id);
  1374. gold_assert(static_cast<size_t>(section_id) < this->section_id_map_.size());
  1375. unsigned int shndx = this->section_id_map_[section_id];
  1376. // Create the section if necessary.
  1377. if (shndx == 0)
  1378. {
  1379. section_name = this->shstrtab_.add_with_length(section_name,
  1380. strlen(section_name),
  1381. false, NULL);
  1382. shndx = this->add_output_section(section_name, align);
  1383. this->section_id_map_[section_id] = shndx;
  1384. }
  1385. Section& section = this->sections_[shndx - 1];
  1386. section_offset_type section_offset;
  1387. if (section_id == elfcpp::DW_SECT_INFO)
  1388. {
  1389. // Write the .debug_info.dwo section directly.
  1390. // We do not need to free the memory in this case.
  1391. off_t file_offset = this->next_file_offset_;
  1392. gold_assert(this->size_ > 0 && file_offset > 0);
  1393. file_offset = align_offset(file_offset, align);
  1394. if (section.offset == 0)
  1395. section.offset = file_offset;
  1396. if (align > section.align)
  1397. {
  1398. // Since we've already committed to the layout for this
  1399. // section, an unexpected large alignment boundary may
  1400. // be impossible to honor.
  1401. if (align_offset(section.offset, align) != section.offset)
  1402. gold_fatal(_("%s: alignment (%d) for section '%s' "
  1403. "cannot be honored"),
  1404. this->name_, align, section_name);
  1405. section.align = align;
  1406. }
  1407. section_offset = file_offset - section.offset;
  1408. section.size = file_offset + len - section.offset;
  1409. ::fseek(this->fd_, file_offset, SEEK_SET);
  1410. if (::fwrite(contents, 1, len, this->fd_) < len)
  1411. gold_fatal(_("%s: error writing section '%s'"), this->name_,
  1412. section_name);
  1413. this->next_file_offset_ = file_offset + len;
  1414. }
  1415. else
  1416. {
  1417. // Collect the contributions and keep track of the total size.
  1418. if (align > section.align)
  1419. section.align = align;
  1420. section_offset = align_offset(section.size, align);
  1421. section.size = section_offset + len;
  1422. Contribution contrib = { section_offset, len, contents };
  1423. section.contributions.push_back(contrib);
  1424. }
  1425. return section_offset;
  1426. }
  1427. // Add a set of .debug_info and related sections to the output file.
  1428. void
  1429. Dwp_output_file::add_cu_set(Unit_set* cu_set)
  1430. {
  1431. uint64_t dwo_id = cu_set->signature;
  1432. unsigned int slot;
  1433. if (!this->cu_index_.find_or_add(dwo_id, &slot))
  1434. this->cu_index_.enter_set(slot, cu_set);
  1435. else
  1436. gold_warning(_("%s: duplicate entry for CU (dwo_id 0x%llx)"),
  1437. this->name_, (unsigned long long)dwo_id);
  1438. }
  1439. // Lookup a type signature and return TRUE if we have already seen it.
  1440. bool
  1441. Dwp_output_file::lookup_tu(uint64_t type_sig)
  1442. {
  1443. this->last_type_sig_ = type_sig;
  1444. return this->tu_index_.find_or_add(type_sig, &this->last_tu_slot_);
  1445. }
  1446. // Add a set of .debug_types and related sections to the output file.
  1447. void
  1448. Dwp_output_file::add_tu_set(Unit_set* tu_set)
  1449. {
  1450. uint64_t type_sig = tu_set->signature;
  1451. unsigned int slot;
  1452. if (type_sig == this->last_type_sig_)
  1453. slot = this->last_tu_slot_;
  1454. else
  1455. this->tu_index_.find_or_add(type_sig, &slot);
  1456. this->tu_index_.enter_set(slot, tu_set);
  1457. }
  1458. // Find a slot in the hash table for SIGNATURE. Return TRUE
  1459. // if the entry already exists.
  1460. bool
  1461. Dwp_output_file::Dwp_index::find_or_add(uint64_t signature,
  1462. unsigned int* slotp)
  1463. {
  1464. if (this->capacity_ == 0)
  1465. this->initialize();
  1466. unsigned int slot =
  1467. static_cast<unsigned int>(signature) & (this->capacity_ - 1);
  1468. unsigned int secondary_hash;
  1469. uint64_t probe = this->hash_table_[slot];
  1470. uint32_t row_index = this->index_table_[slot];
  1471. if (row_index != 0 && probe != signature)
  1472. {
  1473. secondary_hash = (static_cast<unsigned int>(signature >> 32)
  1474. & (this->capacity_ - 1)) | 1;
  1475. do
  1476. {
  1477. slot = (slot + secondary_hash) & (this->capacity_ - 1);
  1478. probe = this->hash_table_[slot];
  1479. row_index = this->index_table_[slot];
  1480. } while (row_index != 0 && probe != signature);
  1481. }
  1482. *slotp = slot;
  1483. return (row_index != 0);
  1484. }
  1485. // Enter a CU or TU set at the given SLOT in the hash table.
  1486. void
  1487. Dwp_output_file::Dwp_index::enter_set(unsigned int slot,
  1488. const Unit_set* set)
  1489. {
  1490. gold_assert(slot < this->capacity_);
  1491. // Add a row to the offsets and sizes tables.
  1492. this->section_table_.push_back(set);
  1493. uint32_t row_index = this->section_table_rows();
  1494. // Mark the sections used in this set.
  1495. for (unsigned int i = 1; i <= elfcpp::DW_SECT_MAX; i++)
  1496. if (set->sections[i].size > 0)
  1497. this->section_mask_ |= 1 << i;
  1498. // Enter the signature and pool index into the hash table.
  1499. gold_assert(this->hash_table_[slot] == 0);
  1500. this->hash_table_[slot] = set->signature;
  1501. this->index_table_[slot] = row_index;
  1502. ++this->used_;
  1503. // Grow the hash table when we exceed 2/3 capacity.
  1504. if (this->used_ * 3 > this->capacity_ * 2)
  1505. this->grow();
  1506. }
  1507. // Initialize the hash table.
  1508. void
  1509. Dwp_output_file::Dwp_index::initialize()
  1510. {
  1511. this->capacity_ = 16;
  1512. this->hash_table_ = new uint64_t[this->capacity_];
  1513. memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t));
  1514. this->index_table_ = new uint32_t[this->capacity_];
  1515. memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t));
  1516. }
  1517. // Grow the hash table when we reach 2/3 capacity.
  1518. void
  1519. Dwp_output_file::Dwp_index::grow()
  1520. {
  1521. unsigned int old_capacity = this->capacity_;
  1522. uint64_t* old_hash_table = this->hash_table_;
  1523. uint32_t* old_index_table = this->index_table_;
  1524. unsigned int old_used = this->used_;
  1525. this->capacity_ = old_capacity * 2;
  1526. this->hash_table_ = new uint64_t[this->capacity_];
  1527. memset(this->hash_table_, 0, this->capacity_ * sizeof(uint64_t));
  1528. this->index_table_ = new uint32_t[this->capacity_];
  1529. memset(this->index_table_, 0, this->capacity_ * sizeof(uint32_t));
  1530. this->used_ = 0;
  1531. for (unsigned int i = 0; i < old_capacity; ++i)
  1532. {
  1533. uint64_t signature = old_hash_table[i];
  1534. uint32_t row_index = old_index_table[i];
  1535. if (row_index != 0)
  1536. {
  1537. unsigned int slot;
  1538. bool found = this->find_or_add(signature, &slot);
  1539. gold_assert(!found);
  1540. this->hash_table_[slot] = signature;
  1541. this->index_table_[slot] = row_index;
  1542. ++this->used_;
  1543. }
  1544. }
  1545. gold_assert(this->used_ == old_used);
  1546. delete[] old_hash_table;
  1547. delete[] old_index_table;
  1548. }
  1549. // Finalize the file, write the string tables and index sections,
  1550. // and close the file.
  1551. void
  1552. Dwp_output_file::finalize()
  1553. {
  1554. unsigned char* buf;
  1555. // Write the accumulated output sections.
  1556. for (unsigned int i = 0; i < this->sections_.size(); i++)
  1557. {
  1558. Section& sect = this->sections_[i];
  1559. // If the offset has already been assigned, the section has been written.
  1560. if (sect.offset > 0 || sect.size == 0)
  1561. continue;
  1562. off_t file_offset = this->next_file_offset_;
  1563. file_offset = align_offset(file_offset, sect.align);
  1564. sect.offset = file_offset;
  1565. this->write_contributions(sect);
  1566. this->next_file_offset_ = file_offset + sect.size;
  1567. }
  1568. // Write the debug string table.
  1569. if (this->have_strings_)
  1570. {
  1571. this->stringpool_.set_string_offsets();
  1572. section_size_type len = this->stringpool_.get_strtab_size();
  1573. buf = new unsigned char[len];
  1574. this->stringpool_.write_to_buffer(buf, len);
  1575. this->write_new_section(".debug_str.dwo", buf, len, 1);
  1576. delete[] buf;
  1577. }
  1578. // Write the CU and TU indexes.
  1579. if (this->big_endian_)
  1580. {
  1581. this->write_index<true>(".debug_cu_index", this->cu_index_);
  1582. this->write_index<true>(".debug_tu_index", this->tu_index_);
  1583. }
  1584. else
  1585. {
  1586. this->write_index<false>(".debug_cu_index", this->cu_index_);
  1587. this->write_index<false>(".debug_tu_index", this->tu_index_);
  1588. }
  1589. off_t file_offset = this->next_file_offset_;
  1590. // Write the section string table.
  1591. this->shstrndx_ = this->shnum_++;
  1592. const char* shstrtab_name =
  1593. this->shstrtab_.add_with_length(".shstrtab", sizeof(".shstrtab") - 1,
  1594. false, NULL);
  1595. this->shstrtab_.set_string_offsets();
  1596. section_size_type shstrtab_len = this->shstrtab_.get_strtab_size();
  1597. buf = new unsigned char[shstrtab_len];
  1598. this->shstrtab_.write_to_buffer(buf, shstrtab_len);
  1599. off_t shstrtab_off = file_offset;
  1600. ::fseek(this->fd_, file_offset, 0);
  1601. if (::fwrite(buf, 1, shstrtab_len, this->fd_) < shstrtab_len)
  1602. gold_fatal(_("%s: error writing section '.shstrtab'"), this->name_);
  1603. delete[] buf;
  1604. file_offset += shstrtab_len;
  1605. // Write the section header table. The first entry is a NULL entry.
  1606. // This is followed by the debug sections, and finally we write the
  1607. // .shstrtab section header.
  1608. file_offset = align_offset(file_offset, this->size_ == 32 ? 4 : 8);
  1609. this->shoff_ = file_offset;
  1610. ::fseek(this->fd_, file_offset, 0);
  1611. section_size_type sh0_size = 0;
  1612. unsigned int sh0_link = 0;
  1613. if (this->shnum_ >= elfcpp::SHN_LORESERVE)
  1614. sh0_size = this->shnum_;
  1615. if (this->shstrndx_ >= elfcpp::SHN_LORESERVE)
  1616. sh0_link = this->shstrndx_;
  1617. this->write_shdr(NULL, 0, 0, 0, 0, sh0_size, sh0_link, 0, 0, 0);
  1618. for (unsigned int i = 0; i < this->sections_.size(); ++i)
  1619. {
  1620. Section& sect = this->sections_[i];
  1621. this->write_shdr(sect.name, elfcpp::SHT_PROGBITS, 0, 0, sect.offset,
  1622. sect.size, 0, 0, sect.align, 0);
  1623. }
  1624. this->write_shdr(shstrtab_name, elfcpp::SHT_STRTAB, 0, 0,
  1625. shstrtab_off, shstrtab_len, 0, 0, 1, 0);
  1626. // Write the ELF header.
  1627. this->write_ehdr();
  1628. // Close the file.
  1629. if (this->fd_ != NULL)
  1630. {
  1631. if (::fclose(this->fd_) != 0)
  1632. gold_fatal(_("%s: %s"), this->name_, strerror(errno));
  1633. }
  1634. this->fd_ = NULL;
  1635. }
  1636. // Write the contributions to an output section.
  1637. void
  1638. Dwp_output_file::write_contributions(const Section& sect)
  1639. {
  1640. for (unsigned int i = 0; i < sect.contributions.size(); ++i)
  1641. {
  1642. const Contribution& c = sect.contributions[i];
  1643. ::fseek(this->fd_, sect.offset + c.output_offset, SEEK_SET);
  1644. if (::fwrite(c.contents, 1, c.size, this->fd_) < c.size)
  1645. gold_fatal(_("%s: error writing section '%s'"), this->name_, sect.name);
  1646. delete[] c.contents;
  1647. }
  1648. }
  1649. // Write a new section to the output file.
  1650. void
  1651. Dwp_output_file::write_new_section(const char* section_name,
  1652. const unsigned char* contents,
  1653. section_size_type len, int align)
  1654. {
  1655. section_name = this->shstrtab_.add_with_length(section_name,
  1656. strlen(section_name),
  1657. false, NULL);
  1658. unsigned int shndx = this->add_output_section(section_name, align);
  1659. Section& section = this->sections_[shndx - 1];
  1660. off_t file_offset = this->next_file_offset_;
  1661. file_offset = align_offset(file_offset, align);
  1662. section.offset = file_offset;
  1663. section.size = len;
  1664. ::fseek(this->fd_, file_offset, SEEK_SET);
  1665. if (::fwrite(contents, 1, len, this->fd_) < len)
  1666. gold_fatal(_("%s: error writing section '%s'"), this->name_, section_name);
  1667. this->next_file_offset_ = file_offset + len;
  1668. }
  1669. // Write a CU or TU index section.
  1670. template<bool big_endian>
  1671. void
  1672. Dwp_output_file::write_index(const char* sect_name, const Dwp_index& index)
  1673. {
  1674. const unsigned int nslots = index.hash_table_total_slots();
  1675. const unsigned int nused = index.hash_table_used_slots();
  1676. const unsigned int nrows = index.section_table_rows();
  1677. int column_mask = index.section_table_cols();
  1678. unsigned int ncols = 0;
  1679. for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c)
  1680. if (column_mask & (1 << c))
  1681. ncols++;
  1682. const unsigned int ntable = (nrows * 2 + 1) * ncols;
  1683. const section_size_type index_size = (4 * sizeof(uint32_t)
  1684. + nslots * sizeof(uint64_t)
  1685. + nslots * sizeof(uint32_t)
  1686. + ntable * sizeof(uint32_t));
  1687. // Allocate a buffer for the section contents.
  1688. unsigned char* buf = new unsigned char[index_size];
  1689. unsigned char* p = buf;
  1690. // Write the section header: version number, padding,
  1691. // number of used slots and total number of slots.
  1692. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, 2);
  1693. p += sizeof(uint32_t);
  1694. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, ncols);
  1695. p += sizeof(uint32_t);
  1696. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nused);
  1697. p += sizeof(uint32_t);
  1698. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, nslots);
  1699. p += sizeof(uint32_t);
  1700. // Write the hash table.
  1701. for (unsigned int i = 0; i < nslots; ++i)
  1702. {
  1703. elfcpp::Swap_unaligned<64, big_endian>::writeval(p, index.hash_table(i));
  1704. p += sizeof(uint64_t);
  1705. }
  1706. // Write the parallel index table.
  1707. for (unsigned int i = 0; i < nslots; ++i)
  1708. {
  1709. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, index.index_table(i));
  1710. p += sizeof(uint32_t);
  1711. }
  1712. // Write the first row of the table of section offsets.
  1713. for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c)
  1714. {
  1715. if (column_mask & (1 << c))
  1716. {
  1717. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, c);
  1718. p += sizeof(uint32_t);
  1719. }
  1720. }
  1721. // Write the table of section offsets.
  1722. Dwp_index::Section_table::const_iterator tbl = index.section_table();
  1723. for (unsigned int r = 0; r < nrows; ++r)
  1724. {
  1725. gold_assert(tbl != index.section_table_end());
  1726. const Section_bounds* sects = (*tbl)->sections;
  1727. for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c)
  1728. {
  1729. if (column_mask & (1 << c))
  1730. {
  1731. section_offset_type offset = sects[c].offset;
  1732. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, offset);
  1733. p += sizeof(uint32_t);
  1734. }
  1735. else
  1736. gold_assert(sects[c].size == 0);
  1737. }
  1738. ++tbl;
  1739. }
  1740. // Write the table of section sizes.
  1741. tbl = index.section_table();
  1742. for (unsigned int r = 0; r < nrows; ++r)
  1743. {
  1744. gold_assert(tbl != index.section_table_end());
  1745. const Section_bounds* sects = (*tbl)->sections;
  1746. for (unsigned int c = 1; c <= elfcpp::DW_SECT_MAX; ++c)
  1747. {
  1748. if (column_mask & (1 << c))
  1749. {
  1750. section_size_type size = sects[c].size;
  1751. elfcpp::Swap_unaligned<32, big_endian>::writeval(p, size);
  1752. p += sizeof(uint32_t);
  1753. }
  1754. else
  1755. gold_assert(sects[c].size == 0);
  1756. }
  1757. ++tbl;
  1758. }
  1759. gold_assert(p == buf + index_size);
  1760. this->write_new_section(sect_name, buf, index_size, sizeof(uint64_t));
  1761. delete[] buf;
  1762. }
  1763. // Write the ELF header.
  1764. void
  1765. Dwp_output_file::write_ehdr()
  1766. {
  1767. if (this->size_ == 32)
  1768. {
  1769. if (this->big_endian_)
  1770. return this->sized_write_ehdr<32, true>();
  1771. else
  1772. return this->sized_write_ehdr<32, false>();
  1773. }
  1774. else if (this->size_ == 64)
  1775. {
  1776. if (this->big_endian_)
  1777. return this->sized_write_ehdr<64, true>();
  1778. else
  1779. return this->sized_write_ehdr<64, false>();
  1780. }
  1781. else
  1782. gold_unreachable();
  1783. }
  1784. template<unsigned int size, bool big_endian>
  1785. void
  1786. Dwp_output_file::sized_write_ehdr()
  1787. {
  1788. const unsigned int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
  1789. unsigned char buf[ehdr_size];
  1790. elfcpp::Ehdr_write<size, big_endian> ehdr(buf);
  1791. unsigned char e_ident[elfcpp::EI_NIDENT];
  1792. memset(e_ident, 0, elfcpp::EI_NIDENT);
  1793. e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
  1794. e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
  1795. e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
  1796. e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
  1797. if (size == 32)
  1798. e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
  1799. else if (size == 64)
  1800. e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
  1801. else
  1802. gold_unreachable();
  1803. e_ident[elfcpp::EI_DATA] = (big_endian
  1804. ? elfcpp::ELFDATA2MSB
  1805. : elfcpp::ELFDATA2LSB);
  1806. e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
  1807. ehdr.put_e_ident(e_ident);
  1808. ehdr.put_e_type(elfcpp::ET_REL);
  1809. ehdr.put_e_machine(this->machine_);
  1810. ehdr.put_e_version(elfcpp::EV_CURRENT);
  1811. ehdr.put_e_entry(0);
  1812. ehdr.put_e_phoff(0);
  1813. ehdr.put_e_shoff(this->shoff_);
  1814. ehdr.put_e_flags(0);
  1815. ehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
  1816. ehdr.put_e_phentsize(0);
  1817. ehdr.put_e_phnum(0);
  1818. ehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
  1819. ehdr.put_e_shnum(this->shnum_ < elfcpp::SHN_LORESERVE ? this->shnum_ : 0);
  1820. ehdr.put_e_shstrndx(this->shstrndx_ < elfcpp::SHN_LORESERVE
  1821. ? this->shstrndx_
  1822. : static_cast<unsigned int>(elfcpp::SHN_XINDEX));
  1823. ::fseek(this->fd_, 0, 0);
  1824. if (::fwrite(buf, 1, ehdr_size, this->fd_) < ehdr_size)
  1825. gold_fatal(_("%s: error writing ELF header"), this->name_);
  1826. }
  1827. // Write a section header.
  1828. void
  1829. Dwp_output_file::write_shdr(const char* name, unsigned int type,
  1830. unsigned int flags, uint64_t addr, off_t offset,
  1831. section_size_type sect_size, unsigned int link,
  1832. unsigned int info, unsigned int align,
  1833. unsigned int ent_size)
  1834. {
  1835. if (this->size_ == 32)
  1836. {
  1837. if (this->big_endian_)
  1838. return this->sized_write_shdr<32, true>(name, type, flags, addr,
  1839. offset, sect_size, link, info,
  1840. align, ent_size);
  1841. else
  1842. return this->sized_write_shdr<32, false>(name, type, flags, addr,
  1843. offset, sect_size, link, info,
  1844. align, ent_size);
  1845. }
  1846. else if (this->size_ == 64)
  1847. {
  1848. if (this->big_endian_)
  1849. return this->sized_write_shdr<64, true>(name, type, flags, addr,
  1850. offset, sect_size, link, info,
  1851. align, ent_size);
  1852. else
  1853. return this->sized_write_shdr<64, false>(name, type, flags, addr,
  1854. offset, sect_size, link, info,
  1855. align, ent_size);
  1856. }
  1857. else
  1858. gold_unreachable();
  1859. }
  1860. template<unsigned int size, bool big_endian>
  1861. void
  1862. Dwp_output_file::sized_write_shdr(const char* name, unsigned int type,
  1863. unsigned int flags, uint64_t addr,
  1864. off_t offset, section_size_type sect_size,
  1865. unsigned int link, unsigned int info,
  1866. unsigned int align, unsigned int ent_size)
  1867. {
  1868. const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  1869. unsigned char buf[shdr_size];
  1870. elfcpp::Shdr_write<size, big_endian> shdr(buf);
  1871. shdr.put_sh_name(name == NULL ? 0 : this->shstrtab_.get_offset(name));
  1872. shdr.put_sh_type(type);
  1873. shdr.put_sh_flags(flags);
  1874. shdr.put_sh_addr(addr);
  1875. shdr.put_sh_offset(offset);
  1876. shdr.put_sh_size(sect_size);
  1877. shdr.put_sh_link(link);
  1878. shdr.put_sh_info(info);
  1879. shdr.put_sh_addralign(align);
  1880. shdr.put_sh_entsize(ent_size);
  1881. if (::fwrite(buf, 1, shdr_size, this->fd_) < shdr_size)
  1882. gold_fatal(_("%s: error writing section header table"), this->name_);
  1883. }
  1884. // Class Dwo_name_info_reader.
  1885. // Visit a compilation unit.
  1886. void
  1887. Dwo_name_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die* die)
  1888. {
  1889. const char* dwo_name = die->string_attribute(elfcpp::DW_AT_GNU_dwo_name);
  1890. if (dwo_name != NULL)
  1891. {
  1892. uint64_t dwo_id = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id);
  1893. this->files_->push_back(Dwo_file_entry(dwo_id, dwo_name));
  1894. }
  1895. }
  1896. // Class Unit_reader.
  1897. // Read the CUs or TUs and add them to the output file.
  1898. void
  1899. Unit_reader::add_units(Dwp_output_file* output_file,
  1900. unsigned int debug_abbrev,
  1901. Section_bounds* sections)
  1902. {
  1903. this->output_file_ = output_file;
  1904. this->sections_ = sections;
  1905. this->set_abbrev_shndx(debug_abbrev);
  1906. this->parse();
  1907. }
  1908. // Visit a compilation unit.
  1909. void
  1910. Unit_reader::visit_compilation_unit(off_t, off_t cu_length, Dwarf_die* die)
  1911. {
  1912. if (cu_length == 0)
  1913. return;
  1914. Unit_set* unit_set = new Unit_set();
  1915. unit_set->signature = die->uint_attribute(elfcpp::DW_AT_GNU_dwo_id);
  1916. for (unsigned int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i)
  1917. unit_set->sections[i] = this->sections_[i];
  1918. // Dwp_output_file::add_contribution writes the .debug_info.dwo section
  1919. // directly to the output file, so we do not need to duplicate the
  1920. // section contents, and add_contribution does not need to free the memory.
  1921. section_offset_type off =
  1922. this->output_file_->add_contribution(elfcpp::DW_SECT_INFO,
  1923. this->buffer_at_offset(0),
  1924. cu_length, 1);
  1925. Section_bounds bounds(off, cu_length);
  1926. unit_set->sections[elfcpp::DW_SECT_INFO] = bounds;
  1927. this->output_file_->add_cu_set(unit_set);
  1928. }
  1929. // Visit a type unit.
  1930. void
  1931. Unit_reader::visit_type_unit(off_t, off_t tu_length, off_t,
  1932. uint64_t signature, Dwarf_die*)
  1933. {
  1934. if (tu_length == 0)
  1935. return;
  1936. if (this->output_file_->lookup_tu(signature))
  1937. return;
  1938. Unit_set* unit_set = new Unit_set();
  1939. unit_set->signature = signature;
  1940. for (unsigned int i = elfcpp::DW_SECT_ABBREV; i <= elfcpp::DW_SECT_MAX; ++i)
  1941. unit_set->sections[i] = this->sections_[i];
  1942. unsigned char* contents = new unsigned char[tu_length];
  1943. memcpy(contents, this->buffer_at_offset(0), tu_length);
  1944. section_offset_type off =
  1945. this->output_file_->add_contribution(elfcpp::DW_SECT_TYPES, contents,
  1946. tu_length, 1);
  1947. Section_bounds bounds(off, tu_length);
  1948. unit_set->sections[elfcpp::DW_SECT_TYPES] = bounds;
  1949. this->output_file_->add_tu_set(unit_set);
  1950. }
  1951. }; // End namespace gold
  1952. using namespace gold;
  1953. // Options.
  1954. enum Dwp_options {
  1955. VERIFY_ONLY = 0x101,
  1956. };
  1957. struct option dwp_options[] =
  1958. {
  1959. { "exec", required_argument, NULL, 'e' },
  1960. { "help", no_argument, NULL, 'h' },
  1961. { "output", required_argument, NULL, 'o' },
  1962. { "verbose", no_argument, NULL, 'v' },
  1963. { "verify-only", no_argument, NULL, VERIFY_ONLY },
  1964. { "version", no_argument, NULL, 'V' },
  1965. { NULL, 0, NULL, 0 }
  1966. };
  1967. // Print usage message and exit.
  1968. static void
  1969. usage(FILE* fd, int exit_status)
  1970. {
  1971. fprintf(fd, _("Usage: %s [options] [file...]\n"), program_name);
  1972. fprintf(fd, _(" -h, --help Print this help message\n"));
  1973. fprintf(fd, _(" -e EXE, --exec EXE Get list of dwo files from EXE"
  1974. " (defaults output to EXE.dwp)\n"));
  1975. fprintf(fd, _(" -o FILE, --output FILE Set output dwp file name\n"));
  1976. fprintf(fd, _(" -v, --verbose Verbose output\n"));
  1977. fprintf(fd, _(" --verify-only Verify output file against"
  1978. " exec file\n"));
  1979. fprintf(fd, _(" -V, --version Print version number\n"));
  1980. // REPORT_BUGS_TO is defined in bfd/bfdver.h.
  1981. const char* report = REPORT_BUGS_TO;
  1982. if (*report != '\0')
  1983. fprintf(fd, _("\nReport bugs to %s\n"), report);
  1984. exit(exit_status);
  1985. }
  1986. // Report version information.
  1987. static void
  1988. print_version()
  1989. {
  1990. // This output is intended to follow the GNU standards.
  1991. printf("GNU dwp %s\n", BFD_VERSION_STRING);
  1992. printf(_("Copyright (C) 2022 Free Software Foundation, Inc.\n"));
  1993. printf(_("\
  1994. This program is free software; you may redistribute it under the terms of\n\
  1995. the GNU General Public License version 3 or (at your option) any later version.\n\
  1996. This program has absolutely no warranty.\n"));
  1997. exit(EXIT_SUCCESS);
  1998. }
  1999. // Main program.
  2000. int
  2001. main(int argc, char** argv)
  2002. {
  2003. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  2004. setlocale(LC_MESSAGES, "");
  2005. #endif
  2006. #if defined (HAVE_SETLOCALE)
  2007. setlocale(LC_CTYPE, "");
  2008. #endif
  2009. bindtextdomain(PACKAGE, LOCALEDIR);
  2010. textdomain(PACKAGE);
  2011. program_name = argv[0];
  2012. // Initialize the global parameters, to let random code get to the
  2013. // errors object.
  2014. Errors errors(program_name);
  2015. set_parameters_errors(&errors);
  2016. // Initialize gold's global options. We don't use these in
  2017. // this program, but they need to be initialized so that
  2018. // functions we call from libgold work properly.
  2019. General_options options;
  2020. set_parameters_options(&options);
  2021. // In libiberty; expands @filename to the args in "filename".
  2022. expandargv(&argc, &argv);
  2023. // Collect file names and options.
  2024. File_list files;
  2025. std::string output_filename;
  2026. const char* exe_filename = NULL;
  2027. bool verbose = false;
  2028. bool verify_only = false;
  2029. int c;
  2030. while ((c = getopt_long(argc, argv, "e:ho:vV", dwp_options, NULL)) != -1)
  2031. {
  2032. switch (c)
  2033. {
  2034. case 'h':
  2035. usage(stdout, EXIT_SUCCESS);
  2036. case 'e':
  2037. exe_filename = optarg;
  2038. break;
  2039. case 'o':
  2040. output_filename.assign(optarg);
  2041. break;
  2042. case 'v':
  2043. verbose = true;
  2044. break;
  2045. case VERIFY_ONLY:
  2046. verify_only = true;
  2047. break;
  2048. case 'V':
  2049. print_version();
  2050. case '?':
  2051. default:
  2052. usage(stderr, EXIT_FAILURE);
  2053. }
  2054. }
  2055. if (output_filename.empty())
  2056. {
  2057. if (exe_filename == NULL)
  2058. gold_fatal(_("no output file specified"));
  2059. output_filename.assign(exe_filename);
  2060. output_filename.append(".dwp");
  2061. }
  2062. // Get list of .dwo files from the executable.
  2063. if (exe_filename != NULL)
  2064. {
  2065. Dwo_file exe_file(exe_filename);
  2066. exe_file.read_executable(&files);
  2067. }
  2068. // Add any additional files listed on command line.
  2069. for (int i = optind; i < argc; ++i)
  2070. files.push_back(Dwo_file_entry(0, argv[i]));
  2071. if (exe_filename == NULL && files.empty())
  2072. gold_fatal(_("no input files and no executable specified"));
  2073. if (verify_only)
  2074. {
  2075. // Get list of DWO files in the DWP file and compare with
  2076. // references found in the EXE file.
  2077. Dwo_file dwp_file(output_filename.c_str());
  2078. bool ok = dwp_file.verify(files);
  2079. return ok ? EXIT_SUCCESS : EXIT_FAILURE;
  2080. }
  2081. // Process each file, adding its contents to the output file.
  2082. Dwp_output_file output_file(output_filename.c_str());
  2083. for (File_list::const_iterator f = files.begin(); f != files.end(); ++f)
  2084. {
  2085. if (verbose)
  2086. fprintf(stderr, "%s\n", f->dwo_name.c_str());
  2087. Dwo_file dwo_file(f->dwo_name.c_str());
  2088. dwo_file.read(&output_file);
  2089. }
  2090. output_file.finalize();
  2091. return EXIT_SUCCESS;
  2092. }