attributes.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // attributes.h -- object attributes for gold -*- C++ -*-
  2. // Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. // Written by Doug Kwan <dougkwan@google.com>.
  4. // This file contains code adapted from BFD.
  5. // This file is part of gold.
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3 of the License, or
  9. // (at your option) any later version.
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. // MA 02110-1301, USA.
  18. // Handle object attributes.
  19. #ifndef GOLD_ATTRIBUTES_H
  20. #define GOLD_ATTRIBUTES_H
  21. #include <map>
  22. #include "parameters.h"
  23. #include "target.h"
  24. #include "output.h"
  25. #include "reduced_debug_output.h"
  26. namespace gold
  27. {
  28. // Object attribute values. The attribute tag is not stored in this object.
  29. class Object_attribute
  30. {
  31. public:
  32. // The value of an object attribute. The type indicates whether the
  33. // attribute holds and integer, a string, or both. It can also indicate that
  34. // there can be no default (i.e. all values must be written to file, even
  35. // zero).
  36. enum
  37. {
  38. ATTR_TYPE_FLAG_INT_VAL = (1 << 0),
  39. ATTR_TYPE_FLAG_STR_VAL = (1 << 1),
  40. ATTR_TYPE_FLAG_NO_DEFAULT = (1 << 2)
  41. };
  42. // Object attributes may either be defined by the processor ABI, index
  43. // OBJ_ATTR_PROC in the *_obj_attributes arrays, or be GNU-specific
  44. // (and possibly also processor-specific), index OBJ_ATTR_GNU.
  45. enum
  46. {
  47. OBJ_ATTR_PROC,
  48. OBJ_ATTR_GNU,
  49. OBJ_ATTR_FIRST = OBJ_ATTR_PROC,
  50. OBJ_ATTR_LAST = OBJ_ATTR_GNU
  51. };
  52. // The following object attribute tags are taken as generic, for all
  53. // targets and for "gnu" where there is no target standard.
  54. enum
  55. {
  56. Tag_NULL = 0,
  57. Tag_File = 1,
  58. Tag_Section = 2,
  59. Tag_Symbol = 3,
  60. Tag_compatibility = 32
  61. };
  62. Object_attribute()
  63. : type_(0), int_value_(0), string_value_()
  64. { }
  65. // Copying constructor. We need to implement this to copy the string value.
  66. Object_attribute(const Object_attribute& oa)
  67. : type_(oa.type_), int_value_(oa.int_value_), string_value_(oa.string_value_)
  68. { }
  69. ~Object_attribute()
  70. { }
  71. // Assignment operator. We need to implement this to copy the string value.
  72. Object_attribute&
  73. operator=(const Object_attribute& source)
  74. {
  75. this->type_ = source.type_;
  76. this->int_value_ = source.int_value_;
  77. this->string_value_ = source.string_value_;
  78. return *this;
  79. }
  80. // Return attribute type.
  81. int
  82. type() const
  83. { return this->type_; }
  84. // Set attribute type.
  85. void
  86. set_type(int type)
  87. { this->type_ = type; }
  88. // Return integer value.
  89. unsigned int
  90. int_value() const
  91. { return this->int_value_; }
  92. // Set integer value.
  93. void
  94. set_int_value(unsigned int i)
  95. { this->int_value_ = i; }
  96. // Return string value.
  97. const std::string&
  98. string_value() const
  99. { return this->string_value_; }
  100. // Set string value.
  101. void
  102. set_string_value(const std::string& s)
  103. { this->string_value_ = s; }
  104. void
  105. set_string_value(const char* s)
  106. { this->string_value_ = s; }
  107. // Whether attribute type has integer value.
  108. static bool
  109. attribute_type_has_int_value(int type)
  110. { return (type & ATTR_TYPE_FLAG_INT_VAL) != 0; }
  111. // Whether attribute type has string value.
  112. static bool
  113. attribute_type_has_string_value(int type)
  114. { return (type & ATTR_TYPE_FLAG_STR_VAL) != 0; }
  115. // Whether attribute type has no default value.
  116. static bool
  117. attribute_type_has_no_default(int type)
  118. { return (type & ATTR_TYPE_FLAG_NO_DEFAULT) != 0; }
  119. // Whether this has default value (0/"").
  120. bool
  121. is_default_attribute() const;
  122. // Return ULEB128 encoded size of tag and attribute.
  123. size_t
  124. size(int tag) const;
  125. // Whether this matches another object attribute in merging.
  126. bool
  127. matches(const Object_attribute& oa) const;
  128. // Write to attribute with tag to BUFFER.
  129. void
  130. write(int tag, std::vector<unsigned char>* buffer) const;
  131. // Determine what arguments an attribute tag takes.
  132. static int
  133. arg_type(int vendor, int tag)
  134. {
  135. switch (vendor)
  136. {
  137. case OBJ_ATTR_PROC:
  138. return parameters->target().attribute_arg_type(tag);
  139. case OBJ_ATTR_GNU:
  140. return Object_attribute::gnu_arg_type(tag);
  141. default:
  142. gold_unreachable();
  143. }
  144. }
  145. private:
  146. // Determine whether a GNU object attribute tag takes an integer, a
  147. // string or both. */
  148. static int
  149. gnu_arg_type(int tag)
  150. {
  151. // Except for Tag_compatibility, for GNU attributes we follow the
  152. // same rule ARM ones > 32 follow: odd-numbered tags take strings
  153. // and even-numbered tags take integers. In addition, tag & 2 is
  154. // nonzero for architecture-independent tags and zero for
  155. // architecture-dependent ones.
  156. if (tag == Object_attribute::Tag_compatibility)
  157. return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
  158. else
  159. return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
  160. }
  161. // Attribute type.
  162. int type_;
  163. // Integer value.
  164. int int_value_;
  165. // String value.
  166. std::string string_value_;
  167. };
  168. // This class contains attributes of a particular vendor.
  169. class Vendor_object_attributes
  170. {
  171. public:
  172. // The maximum number of known object attributes for any target.
  173. static const int NUM_KNOWN_ATTRIBUTES = 71;
  174. Vendor_object_attributes(int vendor)
  175. : vendor_(vendor), other_attributes_()
  176. { }
  177. // Copying constructor.
  178. Vendor_object_attributes(const Vendor_object_attributes&);
  179. ~Vendor_object_attributes()
  180. {
  181. for (Other_attributes::iterator p = this->other_attributes_.begin();
  182. p != this->other_attributes_.end();
  183. ++p)
  184. delete p->second;
  185. }
  186. // Size of this in number of bytes.
  187. size_t
  188. size() const;
  189. // Name of this written vendor subsection.
  190. const char*
  191. name() const
  192. {
  193. return (this->vendor_ == Object_attribute::OBJ_ATTR_PROC
  194. ? parameters->target().attributes_vendor()
  195. : "gnu");
  196. }
  197. // Return an array of known attributes.
  198. Object_attribute*
  199. known_attributes()
  200. { return &this->known_attributes_[0]; }
  201. const Object_attribute*
  202. known_attributes() const
  203. { return &this->known_attributes_[0]; }
  204. typedef std::map<int, Object_attribute*> Other_attributes;
  205. // Return attributes other than the known ones.
  206. Other_attributes*
  207. other_attributes()
  208. { return &this->other_attributes_; }
  209. const Other_attributes*
  210. other_attributes() const
  211. { return &this->other_attributes_; }
  212. // Return a new attribute associated with TAG.
  213. Object_attribute*
  214. new_attribute(int tag);
  215. // Get an attribute
  216. Object_attribute*
  217. get_attribute(int tag);
  218. const Object_attribute*
  219. get_attribute(int tag) const;
  220. // Write to BUFFER.
  221. void
  222. write(std::vector<unsigned char>* buffer) const;
  223. private:
  224. // Vendor of the object attributes.
  225. int vendor_;
  226. // Attributes with known tags. There are store in an array for fast
  227. // access.
  228. Object_attribute known_attributes_[NUM_KNOWN_ATTRIBUTES];
  229. // Attributes with known tags. There are stored in a sorted container.
  230. Other_attributes other_attributes_;
  231. };
  232. // This class contains contents of an attributes section.
  233. class Attributes_section_data
  234. {
  235. public:
  236. // Construct an Attributes_section_data object by parsing section contents
  237. // in VIEW of SIZE.
  238. Attributes_section_data(const unsigned char* view, section_size_type size);
  239. // Copying constructor.
  240. Attributes_section_data(const Attributes_section_data& asd)
  241. {
  242. for (int vendor = Object_attribute::OBJ_ATTR_FIRST;
  243. vendor <= Object_attribute::OBJ_ATTR_LAST;
  244. ++vendor)
  245. this->vendor_object_attributes_[vendor] =
  246. new Vendor_object_attributes(*asd.vendor_object_attributes_[vendor]);
  247. }
  248. ~Attributes_section_data()
  249. {
  250. for (int vendor = Object_attribute::OBJ_ATTR_FIRST;
  251. vendor <= Object_attribute::OBJ_ATTR_LAST;
  252. ++vendor)
  253. delete this->vendor_object_attributes_[vendor];
  254. }
  255. // Return the size of this as number of bytes.
  256. size_t
  257. size() const;
  258. // Return an array of known attributes.
  259. Object_attribute*
  260. known_attributes(int vendor)
  261. {
  262. gold_assert(vendor >= OBJ_ATTR_FIRST && vendor <= OBJ_ATTR_LAST);
  263. return this->vendor_object_attributes_[vendor]->known_attributes();
  264. }
  265. const Object_attribute*
  266. known_attributes(int vendor) const
  267. {
  268. gold_assert(vendor >= OBJ_ATTR_FIRST && vendor <= OBJ_ATTR_LAST);
  269. return this->vendor_object_attributes_[vendor]->known_attributes();
  270. }
  271. // Return the other attributes.
  272. Vendor_object_attributes::Other_attributes*
  273. other_attributes(int vendor)
  274. {
  275. gold_assert(vendor >= OBJ_ATTR_FIRST && vendor <= OBJ_ATTR_LAST);
  276. return this->vendor_object_attributes_[vendor]->other_attributes();
  277. }
  278. // Return the other attributes.
  279. const Vendor_object_attributes::Other_attributes*
  280. other_attributes(int vendor) const
  281. {
  282. gold_assert(vendor >= OBJ_ATTR_FIRST && vendor <= OBJ_ATTR_LAST);
  283. return this->vendor_object_attributes_[vendor]->other_attributes();
  284. }
  285. // Return an attribute.
  286. Object_attribute*
  287. get_attribute(int vendor, int tag)
  288. {
  289. gold_assert(vendor >= OBJ_ATTR_FIRST && vendor <= OBJ_ATTR_LAST);
  290. return this->vendor_object_attributes_[vendor]->get_attribute(tag);
  291. }
  292. const Object_attribute*
  293. get_attribute(int vendor, int tag) const
  294. {
  295. gold_assert(vendor >= OBJ_ATTR_FIRST && vendor <= OBJ_ATTR_LAST);
  296. return this->vendor_object_attributes_[vendor]->get_attribute(tag);
  297. }
  298. // Merge target-independent attributes from another Attributes_section_data
  299. // of an object called NAME.
  300. void
  301. merge(const char* name, const Attributes_section_data* pasd);
  302. // Write to byte stream in an unsigned char vector.
  303. void
  304. write(std::vector<unsigned char>*) const;
  305. private:
  306. // For convenience.
  307. static const int OBJ_ATTR_FIRST = Object_attribute::OBJ_ATTR_FIRST;
  308. static const int OBJ_ATTR_LAST = Object_attribute::OBJ_ATTR_LAST;
  309. // Vendor object attributes.
  310. Vendor_object_attributes* vendor_object_attributes_[OBJ_ATTR_LAST+1];
  311. };
  312. // This class is used for writing out an Attribute_section_data.
  313. class Output_attributes_section_data : public Output_section_data
  314. {
  315. public:
  316. Output_attributes_section_data(const Attributes_section_data& asd)
  317. : Output_section_data(1), attributes_section_data_(asd)
  318. { }
  319. protected:
  320. // Write to a map file.
  321. void
  322. do_print_to_mapfile(Mapfile* mapfile) const
  323. { mapfile->print_output_data(this, _("** attributes")); }
  324. // Write the data to the output file.
  325. void
  326. do_write(Output_file*);
  327. // Set final data size.
  328. void
  329. set_final_data_size()
  330. { this->set_data_size(attributes_section_data_.size()); }
  331. private:
  332. // Attributes_section_data corresponding to this.
  333. const Attributes_section_data& attributes_section_data_;
  334. };
  335. } // End namespace gold.
  336. #endif // !defined(GOLD_ATTRIBUTES_H)