dwarf_reader.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
  2. // Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_DWARF_READER_H
  18. #define GOLD_DWARF_READER_H
  19. #include <vector>
  20. #include <map>
  21. #include <limits.h>
  22. #include <sys/types.h>
  23. #include "elfcpp.h"
  24. #include "elfcpp_swap.h"
  25. #include "dwarf.h"
  26. #include "reloc.h"
  27. namespace gold
  28. {
  29. class Dwarf_info_reader;
  30. struct LineStateMachine;
  31. // This class is used to extract the section index and offset of
  32. // the target of a relocation for a given offset within the section.
  33. class Elf_reloc_mapper
  34. {
  35. public:
  36. Elf_reloc_mapper()
  37. { }
  38. virtual
  39. ~Elf_reloc_mapper()
  40. { }
  41. // Initialize the relocation tracker for section RELOC_SHNDX.
  42. bool
  43. initialize(unsigned int reloc_shndx, unsigned int reloc_type)
  44. { return this->do_initialize(reloc_shndx, reloc_type); }
  45. // Return the next reloc_offset.
  46. off_t
  47. next_offset()
  48. { return this->do_next_offset(); }
  49. // Advance to the next relocation past OFFSET.
  50. void
  51. advance(off_t offset)
  52. { this->do_advance(offset); }
  53. // Return the section index and offset within the section of the target
  54. // of the relocation for RELOC_OFFSET in the referring section.
  55. unsigned int
  56. get_reloc_target(off_t reloc_offset, off_t* target_offset)
  57. { return this->do_get_reloc_target(reloc_offset, target_offset); }
  58. // Checkpoint the current position in the reloc section.
  59. uint64_t
  60. checkpoint() const
  61. { return this->do_checkpoint(); }
  62. // Reset the current position to the CHECKPOINT.
  63. void
  64. reset(uint64_t checkpoint)
  65. { this->do_reset(checkpoint); }
  66. protected:
  67. virtual bool
  68. do_initialize(unsigned int, unsigned int) = 0;
  69. // Return the next reloc_offset.
  70. virtual off_t
  71. do_next_offset() = 0;
  72. // Advance to the next relocation past OFFSET.
  73. virtual void
  74. do_advance(off_t offset) = 0;
  75. virtual unsigned int
  76. do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0;
  77. // Checkpoint the current position in the reloc section.
  78. virtual uint64_t
  79. do_checkpoint() const = 0;
  80. // Reset the current position to the CHECKPOINT.
  81. virtual void
  82. do_reset(uint64_t checkpoint) = 0;
  83. };
  84. template<int size, bool big_endian>
  85. class Sized_elf_reloc_mapper : public Elf_reloc_mapper
  86. {
  87. public:
  88. Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab,
  89. off_t symtab_size)
  90. : object_(object), symtab_(symtab), symtab_size_(symtab_size),
  91. reloc_type_(0), track_relocs_()
  92. { }
  93. protected:
  94. bool
  95. do_initialize(unsigned int reloc_shndx, unsigned int reloc_type);
  96. // Return the next reloc_offset.
  97. virtual off_t
  98. do_next_offset()
  99. { return this->track_relocs_.next_offset(); }
  100. // Advance to the next relocation past OFFSET.
  101. virtual void
  102. do_advance(off_t offset)
  103. { this->track_relocs_.advance(offset); }
  104. unsigned int
  105. do_get_reloc_target(off_t reloc_offset, off_t* target_offset);
  106. // Checkpoint the current position in the reloc section.
  107. uint64_t
  108. do_checkpoint() const
  109. { return this->track_relocs_.checkpoint(); }
  110. // Reset the current position to the CHECKPOINT.
  111. void
  112. do_reset(uint64_t checkpoint)
  113. { this->track_relocs_.reset(checkpoint); }
  114. private:
  115. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  116. // Return the section index of symbol SYMNDX, and copy its value to *VALUE.
  117. // Set *IS_ORDINARY true if the section index is an ordinary section index.
  118. unsigned int
  119. symbol_section(unsigned int symndx, Address* value, bool* is_ordinary);
  120. // The object file.
  121. Object* object_;
  122. // The ELF symbol table.
  123. const unsigned char* symtab_;
  124. // The size of the ELF symbol table.
  125. off_t symtab_size_;
  126. // Type of the relocation section (SHT_REL or SHT_RELA).
  127. unsigned int reloc_type_;
  128. // Relocations for the referring section.
  129. Track_relocs<size, big_endian> track_relocs_;
  130. };
  131. // This class is used to read the abbreviations table from the
  132. // .debug_abbrev section of the object file.
  133. class Dwarf_abbrev_table
  134. {
  135. public:
  136. // An attribute list entry.
  137. struct Attribute
  138. {
  139. Attribute(unsigned int a, unsigned int f)
  140. : attr(a), form(f)
  141. { }
  142. unsigned int attr;
  143. unsigned int form;
  144. };
  145. // An abbrev code entry.
  146. struct Abbrev_code
  147. {
  148. Abbrev_code(unsigned int t, bool hc)
  149. : tag(t), has_children(hc), has_sibling_attribute(false), attributes()
  150. {
  151. this->attributes.reserve(10);
  152. }
  153. void
  154. add_attribute(unsigned int attr, unsigned int form)
  155. {
  156. this->attributes.push_back(Attribute(attr, form));
  157. }
  158. // The DWARF tag.
  159. unsigned int tag;
  160. // True if the DIE has children.
  161. bool has_children : 1;
  162. // True if the DIE has a sibling attribute.
  163. bool has_sibling_attribute : 1;
  164. // The list of attributes and forms.
  165. std::vector<Attribute> attributes;
  166. };
  167. Dwarf_abbrev_table()
  168. : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL),
  169. owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_()
  170. {
  171. memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_));
  172. }
  173. ~Dwarf_abbrev_table()
  174. {
  175. if (this->owns_buffer_ && this->buffer_ != NULL)
  176. delete[] this->buffer_;
  177. this->clear_abbrev_codes();
  178. }
  179. // Read the abbrev table from an object file.
  180. bool
  181. read_abbrevs(Relobj* object,
  182. unsigned int abbrev_shndx,
  183. off_t abbrev_offset)
  184. {
  185. // If we've already read this abbrev table, return immediately.
  186. if (this->abbrev_shndx_ > 0
  187. && this->abbrev_shndx_ == abbrev_shndx
  188. && this->abbrev_offset_ == abbrev_offset)
  189. return true;
  190. return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset);
  191. }
  192. // Return the abbrev code entry for CODE. This is a fast path for
  193. // abbrev codes that are in the direct lookup table. If not found
  194. // there, we call do_get_abbrev() to do the hard work.
  195. const Abbrev_code*
  196. get_abbrev(unsigned int code)
  197. {
  198. if (code < this->low_abbrev_code_max_
  199. && this->low_abbrev_codes_[code] != NULL)
  200. return this->low_abbrev_codes_[code];
  201. return this->do_get_abbrev(code);
  202. }
  203. private:
  204. // Read the abbrev table from an object file.
  205. bool
  206. do_read_abbrevs(Relobj* object,
  207. unsigned int abbrev_shndx,
  208. off_t abbrev_offset);
  209. // Lookup the abbrev code entry for CODE.
  210. const Abbrev_code*
  211. do_get_abbrev(unsigned int code);
  212. // Store an abbrev code entry for CODE.
  213. void
  214. store_abbrev(unsigned int code, const Abbrev_code* entry)
  215. {
  216. if (code < this->low_abbrev_code_max_)
  217. this->low_abbrev_codes_[code] = entry;
  218. else
  219. this->high_abbrev_codes_[code] = entry;
  220. }
  221. // Clear the abbrev code table and release the memory it uses.
  222. void
  223. clear_abbrev_codes();
  224. typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table;
  225. // The section index of the current abbrev table.
  226. unsigned int abbrev_shndx_;
  227. // The offset within the section of the current abbrev table.
  228. off_t abbrev_offset_;
  229. // The buffer containing the .debug_abbrev section.
  230. const unsigned char* buffer_;
  231. const unsigned char* buffer_end_;
  232. // True if this object owns the buffer and needs to delete it.
  233. bool owns_buffer_;
  234. // Pointer to the current position in the buffer.
  235. const unsigned char* buffer_pos_;
  236. // The table of abbrev codes.
  237. // We use a direct-lookup array for low abbrev codes,
  238. // and store the rest in a hash table.
  239. static const unsigned int low_abbrev_code_max_ = 256;
  240. const Abbrev_code* low_abbrev_codes_[low_abbrev_code_max_];
  241. Abbrev_code_table high_abbrev_codes_;
  242. };
  243. // A DWARF range list. The start and end offsets are relative
  244. // to the input section SHNDX. Each range must lie entirely
  245. // within a single section.
  246. class Dwarf_range_list
  247. {
  248. public:
  249. struct Range
  250. {
  251. Range(unsigned int a_shndx, off_t a_start, off_t a_end)
  252. : shndx(a_shndx), start(a_start), end(a_end)
  253. { }
  254. unsigned int shndx;
  255. off_t start;
  256. off_t end;
  257. };
  258. Dwarf_range_list()
  259. : range_list_()
  260. { }
  261. void
  262. add(unsigned int shndx, off_t start, off_t end)
  263. { this->range_list_.push_back(Range(shndx, start, end)); }
  264. size_t
  265. size() const
  266. { return this->range_list_.size(); }
  267. const Range&
  268. operator[](off_t i) const
  269. { return this->range_list_[i]; }
  270. private:
  271. std::vector<Range> range_list_;
  272. };
  273. // This class is used to read the ranges table from the
  274. // .debug_ranges section of the object file.
  275. class Dwarf_ranges_table
  276. {
  277. public:
  278. Dwarf_ranges_table(Dwarf_info_reader* dwinfo)
  279. : dwinfo_(dwinfo), ranges_shndx_(0), ranges_buffer_(NULL),
  280. ranges_buffer_end_(NULL), owns_ranges_buffer_(false),
  281. ranges_reloc_mapper_(NULL), reloc_type_(0), output_section_offset_(0)
  282. { }
  283. ~Dwarf_ranges_table()
  284. {
  285. if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
  286. delete[] this->ranges_buffer_;
  287. if (this->ranges_reloc_mapper_ != NULL)
  288. delete this->ranges_reloc_mapper_;
  289. }
  290. // Read the ranges table from an object file.
  291. bool
  292. read_ranges_table(Relobj* object,
  293. const unsigned char* symtab,
  294. off_t symtab_size,
  295. unsigned int ranges_shndx);
  296. // Read the range table from an object file.
  297. Dwarf_range_list*
  298. read_range_list(Relobj* object,
  299. const unsigned char* symtab,
  300. off_t symtab_size,
  301. unsigned int address_size,
  302. unsigned int ranges_shndx,
  303. off_t ranges_offset);
  304. // Look for a relocation at offset OFF in the range table,
  305. // and return the section index and offset of the target.
  306. unsigned int
  307. lookup_reloc(off_t off, off_t* target_off);
  308. private:
  309. // The Dwarf_info_reader, for reading data.
  310. Dwarf_info_reader* dwinfo_;
  311. // The section index of the ranges table.
  312. unsigned int ranges_shndx_;
  313. // The buffer containing the .debug_ranges section.
  314. const unsigned char* ranges_buffer_;
  315. const unsigned char* ranges_buffer_end_;
  316. // True if this object owns the buffer and needs to delete it.
  317. bool owns_ranges_buffer_;
  318. // Relocation mapper for the .debug_ranges section.
  319. Elf_reloc_mapper* ranges_reloc_mapper_;
  320. // Type of the relocation section (SHT_REL or SHT_RELA).
  321. unsigned int reloc_type_;
  322. // For incremental update links, this will hold the offset of the
  323. // input section within the output section. Offsets read from
  324. // relocated data will be relative to the output section, and need
  325. // to be corrected before reading data from the input section.
  326. uint64_t output_section_offset_;
  327. };
  328. // This class is used to read the pubnames and pubtypes tables from the
  329. // .debug_pubnames and .debug_pubtypes sections of the object file.
  330. class Dwarf_pubnames_table
  331. {
  332. public:
  333. Dwarf_pubnames_table(Dwarf_info_reader* dwinfo, bool is_pubtypes)
  334. : dwinfo_(dwinfo), buffer_(NULL), buffer_end_(NULL), owns_buffer_(false),
  335. offset_size_(0), pinfo_(NULL), end_of_table_(NULL),
  336. is_pubtypes_(is_pubtypes), is_gnu_style_(false),
  337. unit_length_(0), cu_offset_(0)
  338. { }
  339. ~Dwarf_pubnames_table()
  340. {
  341. if (this->owns_buffer_ && this->buffer_ != NULL)
  342. delete[] this->buffer_;
  343. }
  344. // Read the pubnames section from the object file, using the symbol
  345. // table for relocating it.
  346. bool
  347. read_section(Relobj* object, const unsigned char* symbol_table,
  348. off_t symtab_size);
  349. // Read the header for the set at OFFSET.
  350. bool
  351. read_header(off_t offset);
  352. // Return the offset to the cu within the info or types section.
  353. off_t
  354. cu_offset()
  355. { return this->cu_offset_; }
  356. // Return the size of this subsection of the table. The unit length
  357. // doesn't include the size of its own field.
  358. off_t
  359. subsection_size()
  360. { return this->unit_length_; }
  361. // Read the next name from the set. If the pubname table is gnu-style,
  362. // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index.
  363. const char*
  364. next_name(uint8_t* flag_byte);
  365. private:
  366. // The Dwarf_info_reader, for reading data.
  367. Dwarf_info_reader* dwinfo_;
  368. // The buffer containing the .debug_ranges section.
  369. const unsigned char* buffer_;
  370. const unsigned char* buffer_end_;
  371. // True if this object owns the buffer and needs to delete it.
  372. bool owns_buffer_;
  373. // The size of a DWARF offset for the current set.
  374. unsigned int offset_size_;
  375. // The current position within the buffer.
  376. const unsigned char* pinfo_;
  377. // The end of the current pubnames table.
  378. const unsigned char* end_of_table_;
  379. // TRUE if this is a .debug_pubtypes section.
  380. bool is_pubtypes_;
  381. // Gnu-style pubnames table. This style has an extra flag byte between the
  382. // offset and the name, and is used for generating version 7 of gdb-index.
  383. bool is_gnu_style_;
  384. // Fields read from the header.
  385. uint64_t unit_length_;
  386. off_t cu_offset_;
  387. // Track relocations for this table so we can find the CUs that
  388. // correspond to the subsections.
  389. Elf_reloc_mapper* reloc_mapper_;
  390. // Type of the relocation section (SHT_REL or SHT_RELA).
  391. unsigned int reloc_type_;
  392. };
  393. // This class represents a DWARF Debug Info Entry (DIE).
  394. class Dwarf_die
  395. {
  396. public:
  397. // An attribute value.
  398. struct Attribute_value
  399. {
  400. unsigned int attr;
  401. unsigned int form;
  402. union
  403. {
  404. int64_t intval;
  405. uint64_t uintval;
  406. const char* stringval;
  407. const unsigned char* blockval;
  408. off_t refval;
  409. } val;
  410. union
  411. {
  412. // Section index for reference forms.
  413. unsigned int shndx;
  414. // Block length for block forms.
  415. unsigned int blocklen;
  416. // Attribute offset for DW_FORM_strp.
  417. unsigned int attr_off;
  418. } aux;
  419. };
  420. // A list of attribute values.
  421. typedef std::vector<Attribute_value> Attributes;
  422. Dwarf_die(Dwarf_info_reader* dwinfo,
  423. off_t die_offset,
  424. Dwarf_die* parent);
  425. // Return the DWARF tag for this DIE.
  426. unsigned int
  427. tag() const
  428. {
  429. if (this->abbrev_code_ == NULL)
  430. return 0;
  431. return this->abbrev_code_->tag;
  432. }
  433. // Return true if this DIE has children.
  434. bool
  435. has_children() const
  436. {
  437. gold_assert(this->abbrev_code_ != NULL);
  438. return this->abbrev_code_->has_children;
  439. }
  440. // Return true if this DIE has a sibling attribute.
  441. bool
  442. has_sibling_attribute() const
  443. {
  444. gold_assert(this->abbrev_code_ != NULL);
  445. return this->abbrev_code_->has_sibling_attribute;
  446. }
  447. // Return the value of attribute ATTR.
  448. const Attribute_value*
  449. attribute(unsigned int attr);
  450. // Return the value of the DW_AT_name attribute.
  451. const char*
  452. name()
  453. {
  454. if (this->name_ == NULL)
  455. this->set_name();
  456. return this->name_;
  457. }
  458. // Return the value of the DW_AT_linkage_name
  459. // or DW_AT_MIPS_linkage_name attribute.
  460. const char*
  461. linkage_name()
  462. {
  463. if (this->linkage_name_ == NULL)
  464. this->set_linkage_name();
  465. return this->linkage_name_;
  466. }
  467. // Return the value of the DW_AT_specification attribute.
  468. off_t
  469. specification()
  470. {
  471. if (!this->attributes_read_)
  472. this->read_attributes();
  473. return this->specification_;
  474. }
  475. // Return the value of the DW_AT_abstract_origin attribute.
  476. off_t
  477. abstract_origin()
  478. {
  479. if (!this->attributes_read_)
  480. this->read_attributes();
  481. return this->abstract_origin_;
  482. }
  483. // Return the value of attribute ATTR as a string.
  484. const char*
  485. string_attribute(unsigned int attr);
  486. // Return the value of attribute ATTR as an integer.
  487. int64_t
  488. int_attribute(unsigned int attr);
  489. // Return the value of attribute ATTR as an unsigned integer.
  490. uint64_t
  491. uint_attribute(unsigned int attr);
  492. // Return the value of attribute ATTR as a reference.
  493. off_t
  494. ref_attribute(unsigned int attr, unsigned int* shndx);
  495. // Return the value of attribute ATTR as a address.
  496. off_t
  497. address_attribute(unsigned int attr, unsigned int* shndx);
  498. // Return the value of attribute ATTR as a flag.
  499. bool
  500. flag_attribute(unsigned int attr)
  501. { return this->int_attribute(attr) != 0; }
  502. // Return true if this DIE is a declaration.
  503. bool
  504. is_declaration()
  505. { return this->flag_attribute(elfcpp::DW_AT_declaration); }
  506. // Return the parent of this DIE.
  507. Dwarf_die*
  508. parent() const
  509. { return this->parent_; }
  510. // Return the offset of this DIE.
  511. off_t
  512. offset() const
  513. { return this->die_offset_; }
  514. // Return the offset of this DIE's first child.
  515. off_t
  516. child_offset();
  517. // Set the offset of this DIE's next sibling.
  518. void
  519. set_sibling_offset(off_t sibling_offset)
  520. { this->sibling_offset_ = sibling_offset; }
  521. // Return the offset of this DIE's next sibling.
  522. off_t
  523. sibling_offset();
  524. private:
  525. typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code;
  526. // Read all the attributes of the DIE.
  527. bool
  528. read_attributes();
  529. // Set the name of the DIE if present.
  530. void
  531. set_name();
  532. // Set the linkage name if present.
  533. void
  534. set_linkage_name();
  535. // Skip all the attributes of the DIE and return the offset
  536. // of the next DIE.
  537. off_t
  538. skip_attributes();
  539. // The Dwarf_info_reader, for reading attributes.
  540. Dwarf_info_reader* dwinfo_;
  541. // The parent of this DIE.
  542. Dwarf_die* parent_;
  543. // Offset of this DIE within its compilation unit.
  544. off_t die_offset_;
  545. // Offset of the first attribute, relative to the beginning of the DIE.
  546. off_t attr_offset_;
  547. // Offset of the first child, relative to the compilation unit.
  548. off_t child_offset_;
  549. // Offset of the next sibling, relative to the compilation unit.
  550. off_t sibling_offset_;
  551. // The abbreviation table entry.
  552. const Abbrev_code* abbrev_code_;
  553. // The list of attributes.
  554. Attributes attributes_;
  555. // True if the attributes have been read.
  556. bool attributes_read_;
  557. // The following fields hold common attributes to avoid a linear
  558. // search through the attribute list.
  559. // The DIE name (DW_AT_name).
  560. const char* name_;
  561. // Offset of the name in the string table (for DW_FORM_strp).
  562. off_t name_off_;
  563. // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name).
  564. const char* linkage_name_;
  565. // Offset of the linkage name in the string table (for DW_FORM_strp).
  566. off_t linkage_name_off_;
  567. // Section index of the string table (for DW_FORM_strp).
  568. unsigned int string_shndx_;
  569. // The value of a DW_AT_specification attribute.
  570. off_t specification_;
  571. // The value of a DW_AT_abstract_origin attribute.
  572. off_t abstract_origin_;
  573. };
  574. // This class is used to read the debug info from the .debug_info
  575. // or .debug_types sections. This is a base class that implements
  576. // the generic parsing of the compilation unit header and DIE
  577. // structure. The parse() method parses the entire section, and
  578. // calls the various visit_xxx() methods for each header. Clients
  579. // should derive a new class from this one and implement the
  580. // visit_compilation_unit() and visit_type_unit() functions.
  581. class Dwarf_info_reader
  582. {
  583. public:
  584. Dwarf_info_reader(bool is_type_unit,
  585. Relobj* object,
  586. const unsigned char* symtab,
  587. off_t symtab_size,
  588. unsigned int shndx,
  589. unsigned int reloc_shndx,
  590. unsigned int reloc_type)
  591. : is_type_unit_(is_type_unit), object_(object), symtab_(symtab),
  592. symtab_size_(symtab_size), shndx_(shndx), reloc_shndx_(reloc_shndx),
  593. reloc_type_(reloc_type), abbrev_shndx_(0), string_shndx_(0),
  594. buffer_(NULL), buffer_end_(NULL), cu_offset_(0), cu_length_(0),
  595. offset_size_(0), address_size_(0), cu_version_(0),
  596. abbrev_table_(), ranges_table_(this),
  597. reloc_mapper_(NULL), string_buffer_(NULL), string_buffer_end_(NULL),
  598. owns_string_buffer_(false), string_output_section_offset_(0)
  599. { }
  600. virtual
  601. ~Dwarf_info_reader()
  602. {
  603. if (this->reloc_mapper_ != NULL)
  604. delete this->reloc_mapper_;
  605. if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
  606. delete[] this->string_buffer_;
  607. }
  608. // Begin parsing the debug info. This calls visit_compilation_unit()
  609. // or visit_type_unit() for each compilation or type unit found in the
  610. // section, and visit_die() for each top-level DIE.
  611. void
  612. parse();
  613. // Return the abbrev code entry for a CODE.
  614. const Dwarf_abbrev_table::Abbrev_code*
  615. get_abbrev(unsigned int code)
  616. { return this->abbrev_table_.get_abbrev(code); }
  617. // Return a pointer to the DWARF info buffer at OFFSET.
  618. const unsigned char*
  619. buffer_at_offset(off_t offset) const
  620. {
  621. const unsigned char* p = this->buffer_ + this->cu_offset_ + offset;
  622. if (this->check_buffer(p + 1))
  623. return p;
  624. return NULL;
  625. }
  626. // Read a possibly unaligned integer of SIZE.
  627. template <int valsize>
  628. inline typename elfcpp::Valtype_base<valsize>::Valtype
  629. read_from_pointer(const unsigned char* source);
  630. // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
  631. template <int valsize>
  632. inline typename elfcpp::Valtype_base<valsize>::Valtype
  633. read_from_pointer(const unsigned char** source);
  634. // Look for a relocation at offset ATTR_OFF in the dwarf info,
  635. // and return the section index and offset of the target.
  636. unsigned int
  637. lookup_reloc(off_t attr_off, off_t* target_off);
  638. // Return a string from the DWARF string table.
  639. const char*
  640. get_string(off_t str_off, unsigned int string_shndx);
  641. // Return the size of a DWARF offset.
  642. unsigned int
  643. offset_size() const
  644. { return this->offset_size_; }
  645. // Return the size of an address.
  646. unsigned int
  647. address_size() const
  648. { return this->address_size_; }
  649. // Set the section index of the .debug_abbrev section.
  650. // We use this if there are no relocations for the .debug_info section.
  651. // If not set, the code parse() routine will search for the section by name.
  652. void
  653. set_abbrev_shndx(unsigned int abbrev_shndx)
  654. { this->abbrev_shndx_ = abbrev_shndx; }
  655. // Return a pointer to the object file's ELF symbol table.
  656. const unsigned char*
  657. symtab() const
  658. { return this->symtab_; }
  659. // Return the size of the object file's ELF symbol table.
  660. off_t
  661. symtab_size() const
  662. { return this->symtab_size_; }
  663. // Return the offset of the current compilation unit.
  664. off_t
  665. cu_offset() const
  666. { return this->cu_offset_; }
  667. protected:
  668. // Begin parsing the debug info. This calls visit_compilation_unit()
  669. // or visit_type_unit() for each compilation or type unit found in the
  670. // section, and visit_die() for each top-level DIE.
  671. template<bool big_endian>
  672. void
  673. do_parse();
  674. // The following methods are hooks that are meant to be implemented
  675. // by a derived class. A default, do-nothing, implementation of
  676. // each is provided for this base class.
  677. // Visit a compilation unit.
  678. virtual void
  679. visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die);
  680. // Visit a type unit.
  681. virtual void
  682. visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
  683. uint64_t signature, Dwarf_die* root_die);
  684. // Read the range table.
  685. Dwarf_range_list*
  686. read_range_list(unsigned int ranges_shndx, off_t ranges_offset)
  687. {
  688. return this->ranges_table_.read_range_list(this->object_,
  689. this->symtab_,
  690. this->symtab_size_,
  691. this->address_size_,
  692. ranges_shndx,
  693. ranges_offset);
  694. }
  695. // Return the object.
  696. Relobj*
  697. object() const
  698. { return this->object_; }
  699. // Checkpoint the relocation tracker.
  700. uint64_t
  701. get_reloc_checkpoint() const
  702. { return this->reloc_mapper_->checkpoint(); }
  703. // Reset the relocation tracker to the CHECKPOINT.
  704. void
  705. reset_relocs(uint64_t checkpoint)
  706. { this->reloc_mapper_->reset(checkpoint); }
  707. private:
  708. // Print a warning about a corrupt debug section.
  709. void
  710. warn_corrupt_debug_section() const;
  711. // Check that P is within the bounds of the current section.
  712. bool
  713. check_buffer(const unsigned char* p) const
  714. {
  715. if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
  716. {
  717. this->warn_corrupt_debug_section();
  718. return false;
  719. }
  720. return true;
  721. }
  722. // Read the DWARF string table.
  723. bool
  724. read_string_table(unsigned int string_shndx)
  725. {
  726. // If we've already read this string table, return immediately.
  727. if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx)
  728. return true;
  729. if (string_shndx == 0 && this->string_shndx_ > 0)
  730. return true;
  731. return this->do_read_string_table(string_shndx);
  732. }
  733. bool
  734. do_read_string_table(unsigned int string_shndx);
  735. // True if this is a type unit; false for a compilation unit.
  736. bool is_type_unit_;
  737. // The object containing the .debug_info or .debug_types input section.
  738. Relobj* object_;
  739. // The ELF symbol table.
  740. const unsigned char* symtab_;
  741. // The size of the ELF symbol table.
  742. off_t symtab_size_;
  743. // Index of the .debug_info or .debug_types section.
  744. unsigned int shndx_;
  745. // Index of the relocation section.
  746. unsigned int reloc_shndx_;
  747. // Type of the relocation section (SHT_REL or SHT_RELA).
  748. unsigned int reloc_type_;
  749. // Index of the .debug_abbrev section (0 if not known).
  750. unsigned int abbrev_shndx_;
  751. // Index of the .debug_str section.
  752. unsigned int string_shndx_;
  753. // The buffer for the debug info.
  754. const unsigned char* buffer_;
  755. const unsigned char* buffer_end_;
  756. // Offset of the current compilation unit.
  757. off_t cu_offset_;
  758. // Length of the current compilation unit.
  759. off_t cu_length_;
  760. // Size of a DWARF offset for the current compilation unit.
  761. unsigned int offset_size_;
  762. // Size of an address for the target architecture.
  763. unsigned int address_size_;
  764. // Compilation unit version number.
  765. unsigned int cu_version_;
  766. // Abbreviations table for current compilation unit.
  767. Dwarf_abbrev_table abbrev_table_;
  768. // Ranges table for the current compilation unit.
  769. Dwarf_ranges_table ranges_table_;
  770. // Relocation mapper for the section.
  771. Elf_reloc_mapper* reloc_mapper_;
  772. // The buffer for the debug string table.
  773. const char* string_buffer_;
  774. const char* string_buffer_end_;
  775. // True if this object owns the buffer and needs to delete it.
  776. bool owns_string_buffer_;
  777. // For incremental update links, this will hold the offset of the
  778. // input .debug_str section within the output section. Offsets read
  779. // from relocated data will be relative to the output section, and need
  780. // to be corrected before reading data from the input section.
  781. uint64_t string_output_section_offset_;
  782. };
  783. // We can't do better than to keep the offsets in a sorted vector.
  784. // Here, offset is the key, and file_num/line_num is the value.
  785. struct Offset_to_lineno_entry
  786. {
  787. off_t offset;
  788. int header_num; // which file-list to use (i.e. which .o file are we in)
  789. // A pointer into files_.
  790. unsigned int file_num : sizeof(int) * CHAR_BIT - 1;
  791. // True if this was the last entry for the current offset, meaning
  792. // it's the line that actually applies.
  793. unsigned int last_line_for_offset : 1;
  794. // The line number in the source file. -1 to indicate end-of-function.
  795. int line_num;
  796. // This sorts by offsets first, and then puts the correct line to
  797. // report for a given offset at the beginning of the run of equal
  798. // offsets (so that asking for 1 line gives the best answer). This
  799. // is not a total ordering.
  800. bool operator<(const Offset_to_lineno_entry& that) const
  801. {
  802. if (this->offset != that.offset)
  803. return this->offset < that.offset;
  804. // Note the '>' which makes this sort 'true' first.
  805. return this->last_line_for_offset > that.last_line_for_offset;
  806. }
  807. };
  808. // This class is used to read the line information from the debugging
  809. // section of an object file.
  810. class Dwarf_line_info
  811. {
  812. public:
  813. Dwarf_line_info()
  814. { }
  815. virtual
  816. ~Dwarf_line_info()
  817. { }
  818. // Given a section number and an offset, returns the associated
  819. // file and line-number, as a string: "file:lineno". If unable
  820. // to do the mapping, returns the empty string. You must call
  821. // read_line_mappings() before calling this function. If
  822. // 'other_lines' is non-NULL, fills that in with other line
  823. // numbers assigned to the same offset.
  824. std::string
  825. addr2line(unsigned int shndx, off_t offset,
  826. std::vector<std::string>* other_lines)
  827. { return this->do_addr2line(shndx, offset, other_lines); }
  828. // A helper function for a single addr2line lookup. It also keeps a
  829. // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
  830. // set to 0 not to cache at all. The larger CACHE_SIZE is, the more
  831. // chance this routine won't have to re-create a Dwarf_line_info
  832. // object for its addr2line computation; such creations are slow.
  833. // NOTE: Not thread-safe, so only call from one thread at a time.
  834. static std::string
  835. one_addr2line(Object* object, unsigned int shndx, off_t offset,
  836. size_t cache_size, std::vector<std::string>* other_lines);
  837. // This reclaims all the memory that one_addr2line may have cached.
  838. // Use this when you know you will not be calling one_addr2line again.
  839. static void
  840. clear_addr2line_cache();
  841. private:
  842. virtual std::string
  843. do_addr2line(unsigned int shndx, off_t offset,
  844. std::vector<std::string>* other_lines) = 0;
  845. };
  846. template<int size, bool big_endian>
  847. class Sized_dwarf_line_info : public Dwarf_line_info
  848. {
  849. public:
  850. // Initializes a .debug_line reader for a given object file.
  851. // If SHNDX is specified and non-negative, only read the debug
  852. // information that pertains to the specified section.
  853. Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
  854. virtual
  855. ~Sized_dwarf_line_info()
  856. {
  857. if (this->buffer_start_ != NULL)
  858. delete[] this->buffer_start_;
  859. }
  860. private:
  861. std::string
  862. do_addr2line(unsigned int shndx, off_t offset,
  863. std::vector<std::string>* other_lines);
  864. // Formats a file and line number to a string like "dirname/filename:lineno".
  865. std::string
  866. format_file_lineno(const Offset_to_lineno_entry& lineno) const;
  867. // Start processing line info, and populates the offset_map_.
  868. // If SHNDX is non-negative, only store debug information that
  869. // pertains to the specified section.
  870. void
  871. read_line_mappings(unsigned int shndx);
  872. // Reads the relocation section associated with .debug_line and
  873. // stores relocation information in reloc_map_.
  874. void
  875. read_relocs();
  876. // Reads the DWARF2/3 header for this line info. Each takes as input
  877. // a starting buffer position, and returns the ending position.
  878. const unsigned char*
  879. read_header_prolog(const unsigned char* lineptr);
  880. const unsigned char*
  881. read_header_tables(const unsigned char* lineptr);
  882. // Reads the DWARF2/3 line information. If shndx is non-negative,
  883. // discard all line information that doesn't pertain to the given
  884. // section.
  885. const unsigned char*
  886. read_lines(const unsigned char* lineptr, unsigned int shndx);
  887. // Process a single line info opcode at START using the state
  888. // machine at LSM. Return true if we should define a line using the
  889. // current state of the line state machine. Place the length of the
  890. // opcode in LEN.
  891. bool
  892. process_one_opcode(const unsigned char* start,
  893. struct LineStateMachine* lsm, size_t* len);
  894. // Some parts of processing differ depending on whether the input
  895. // was a .o file or not.
  896. bool input_is_relobj();
  897. // If we saw anything amiss while parsing, we set this to false.
  898. // Then addr2line will always fail (rather than return possibly-
  899. // corrupt data).
  900. bool data_valid_;
  901. // A DWARF2/3 line info header. This is not the same size as in the
  902. // actual file, as the one in the file may have a 32 bit or 64 bit
  903. // lengths.
  904. struct Dwarf_line_infoHeader
  905. {
  906. off_t total_length;
  907. int version;
  908. off_t prologue_length;
  909. int min_insn_length; // insn stands for instructin
  910. bool default_is_stmt; // stmt stands for statement
  911. signed char line_base;
  912. int line_range;
  913. unsigned char opcode_base;
  914. std::vector<unsigned char> std_opcode_lengths;
  915. int offset_size;
  916. } header_;
  917. // buffer is the buffer for our line info, starting at exactly where
  918. // the line info to read is.
  919. const unsigned char* buffer_;
  920. const unsigned char* buffer_end_;
  921. // If the buffer was allocated temporarily, and therefore must be
  922. // deallocated in the dtor, this contains a pointer to the start
  923. // of the buffer.
  924. const unsigned char* buffer_start_;
  925. // This has relocations that point into buffer.
  926. Sized_elf_reloc_mapper<size, big_endian>* reloc_mapper_;
  927. // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
  928. unsigned int track_relocs_type_;
  929. // This is used to figure out what section to apply a relocation to.
  930. const unsigned char* symtab_buffer_;
  931. section_size_type symtab_buffer_size_;
  932. // Holds the directories and files as we see them. We have an array
  933. // of directory-lists, one for each .o file we're reading (usually
  934. // there will just be one, but there may be more if input is a .so).
  935. std::vector<std::vector<std::string> > directories_;
  936. // The first part is an index into directories_, the second the filename.
  937. std::vector<std::vector< std::pair<int, std::string> > > files_;
  938. // An index into the current directories_ and files_ vectors.
  939. int current_header_index_;
  940. // A sorted map from offset of the relocation target to the shndx
  941. // and addend for the relocation.
  942. typedef std::map<off_t, std::pair<unsigned int, off_t> >
  943. Reloc_map;
  944. Reloc_map reloc_map_;
  945. // We have a vector of offset->lineno entries for every input section.
  946. typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
  947. Lineno_map;
  948. Lineno_map line_number_map_;
  949. };
  950. } // End namespace gold.
  951. #endif // !defined(GOLD_DWARF_READER_H)