1 // attributes.h -- object attributes for gold -*- C++ -*-
3 // Copyright (C) 2009-2017 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com>.
5 // This file contains code adapted from BFD.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
24 // Handle object attributes.
26 #ifndef GOLD_ATTRIBUTES_H
27 #define GOLD_ATTRIBUTES_H
31 #include "parameters.h"
34 #include "reduced_debug_output.h"
39 // Object attribute values. The attribute tag is not stored in this object.
41 class Object_attribute
44 // The value of an object attribute. The type indicates whether the
45 // attribute holds and integer, a string, or both. It can also indicate that
46 // there can be no default (i.e. all values must be written to file, even
50 ATTR_TYPE_FLAG_INT_VAL
= (1 << 0),
51 ATTR_TYPE_FLAG_STR_VAL
= (1 << 1),
52 ATTR_TYPE_FLAG_NO_DEFAULT
= (1 << 2)
55 // Object attributes may either be defined by the processor ABI, index
56 // OBJ_ATTR_PROC in the *_obj_attributes arrays, or be GNU-specific
57 // (and possibly also processor-specific), index OBJ_ATTR_GNU.
62 OBJ_ATTR_FIRST
= OBJ_ATTR_PROC
,
63 OBJ_ATTR_LAST
= OBJ_ATTR_GNU
66 // The following object attribute tags are taken as generic, for all
67 // targets and for "gnu" where there is no target standard.
74 Tag_compatibility
= 32
78 : type_(0), int_value_(0), string_value_()
81 // Copying constructor. We need to implement this to copy the string value.
82 Object_attribute(const Object_attribute
& oa
)
83 : type_(oa
.type_
), int_value_(oa
.int_value_
), string_value_(oa
.string_value_
)
89 // Assignment operator. We need to implement this to copy the string value.
91 operator=(const Object_attribute
& source
)
93 this->type_
= source
.type_
;
94 this->int_value_
= source
.int_value_
;
95 this->string_value_
= source
.string_value_
;
99 // Return attribute type.
102 { return this->type_
; }
104 // Set attribute type.
107 { this->type_
= type
; }
109 // Return integer value.
112 { return this->int_value_
; }
114 // Set integer value.
116 set_int_value(unsigned int i
)
117 { this->int_value_
= i
; }
119 // Return string value.
122 { return this->string_value_
; }
126 set_string_value(const std::string
& s
)
127 { this->string_value_
= s
; }
130 set_string_value(const char* s
)
131 { this->string_value_
= s
; }
133 // Whether attribute type has integer value.
135 attribute_type_has_int_value(int type
)
136 { return (type
& ATTR_TYPE_FLAG_INT_VAL
) != 0; }
138 // Whether attribute type has string value.
140 attribute_type_has_string_value(int type
)
141 { return (type
& ATTR_TYPE_FLAG_STR_VAL
) != 0; }
143 // Whether attribute type has no default value.
145 attribute_type_has_no_default(int type
)
146 { return (type
& ATTR_TYPE_FLAG_NO_DEFAULT
) != 0; }
148 // Whether this has default value (0/"").
150 is_default_attribute() const;
152 // Return ULEB128 encoded size of tag and attribute.
156 // Whether this matches another object attribute in merging.
158 matches(const Object_attribute
& oa
) const;
160 // Write to attribute with tag to BUFFER.
162 write(int tag
, std::vector
<unsigned char>* buffer
) const;
164 // Determine what arguments an attribute tag takes.
166 arg_type(int vendor
, int tag
)
171 return parameters
->target().attribute_arg_type(tag
);
173 return Object_attribute::gnu_arg_type(tag
);
180 // Determine whether a GNU object attribute tag takes an integer, a
181 // string or both. */
183 gnu_arg_type(int tag
)
185 // Except for Tag_compatibility, for GNU attributes we follow the
186 // same rule ARM ones > 32 follow: odd-numbered tags take strings
187 // and even-numbered tags take integers. In addition, tag & 2 is
188 // nonzero for architecture-independent tags and zero for
189 // architecture-dependent ones.
190 if (tag
== Object_attribute::Tag_compatibility
)
191 return ATTR_TYPE_FLAG_INT_VAL
| ATTR_TYPE_FLAG_STR_VAL
;
193 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
201 std::string string_value_
;
204 // This class contains attributes of a particular vendor.
206 class Vendor_object_attributes
209 // The maximum number of known object attributes for any target.
210 static const int NUM_KNOWN_ATTRIBUTES
= 71;
212 Vendor_object_attributes(int vendor
)
213 : vendor_(vendor
), other_attributes_()
216 // Copying constructor.
217 Vendor_object_attributes(const Vendor_object_attributes
&);
219 ~Vendor_object_attributes()
221 for (Other_attributes::iterator p
= this->other_attributes_
.begin();
222 p
!= this->other_attributes_
.end();
227 // Size of this in number of bytes.
231 // Name of this written vendor subsection.
235 return (this->vendor_
== Object_attribute::OBJ_ATTR_PROC
236 ? parameters
->target().attributes_vendor()
240 // Return an array of known attributes.
243 { return &this->known_attributes_
[0]; }
245 const Object_attribute
*
246 known_attributes() const
247 { return &this->known_attributes_
[0]; }
249 typedef std::map
<int, Object_attribute
*> Other_attributes
;
251 // Return attributes other than the known ones.
254 { return &this->other_attributes_
; }
256 const Other_attributes
*
257 other_attributes() const
258 { return &this->other_attributes_
; }
260 // Return a new attribute associated with TAG.
262 new_attribute(int tag
);
266 get_attribute(int tag
);
268 const Object_attribute
*
269 get_attribute(int tag
) const;
273 write(std::vector
<unsigned char>* buffer
) const;
276 // Vendor of the object attributes.
278 // Attributes with known tags. There are store in an array for fast
280 Object_attribute known_attributes_
[NUM_KNOWN_ATTRIBUTES
];
281 // Attributes with known tags. There are stored in a sorted container.
282 Other_attributes other_attributes_
;
285 // This class contains contents of an attributes section.
287 class Attributes_section_data
290 // Construct an Attributes_section_data object by parsing section contents
292 Attributes_section_data(const unsigned char* view
, section_size_type size
);
294 // Copying constructor.
295 Attributes_section_data(const Attributes_section_data
& asd
)
297 for (int vendor
= Object_attribute::OBJ_ATTR_FIRST
;
298 vendor
<= Object_attribute::OBJ_ATTR_LAST
;
300 this->vendor_object_attributes_
[vendor
] =
301 new Vendor_object_attributes(*asd
.vendor_object_attributes_
[vendor
]);
304 ~Attributes_section_data()
306 for (int vendor
= Object_attribute::OBJ_ATTR_FIRST
;
307 vendor
<= Object_attribute::OBJ_ATTR_LAST
;
309 delete this->vendor_object_attributes_
[vendor
];
312 // Return the size of this as number of bytes.
316 // Return an array of known attributes.
318 known_attributes(int vendor
)
320 gold_assert(vendor
>= OBJ_ATTR_FIRST
&& vendor
<= OBJ_ATTR_LAST
);
321 return this->vendor_object_attributes_
[vendor
]->known_attributes();
324 const Object_attribute
*
325 known_attributes(int vendor
) const
327 gold_assert(vendor
>= OBJ_ATTR_FIRST
&& vendor
<= OBJ_ATTR_LAST
);
328 return this->vendor_object_attributes_
[vendor
]->known_attributes();
331 // Return the other attributes.
332 Vendor_object_attributes::Other_attributes
*
333 other_attributes(int vendor
)
335 gold_assert(vendor
>= OBJ_ATTR_FIRST
&& vendor
<= OBJ_ATTR_LAST
);
336 return this->vendor_object_attributes_
[vendor
]->other_attributes();
339 // Return the other attributes.
340 const Vendor_object_attributes::Other_attributes
*
341 other_attributes(int vendor
) const
343 gold_assert(vendor
>= OBJ_ATTR_FIRST
&& vendor
<= OBJ_ATTR_LAST
);
344 return this->vendor_object_attributes_
[vendor
]->other_attributes();
347 // Return an attribute.
349 get_attribute(int vendor
, int tag
)
351 gold_assert(vendor
>= OBJ_ATTR_FIRST
&& vendor
<= OBJ_ATTR_LAST
);
352 return this->vendor_object_attributes_
[vendor
]->get_attribute(tag
);
355 const Object_attribute
*
356 get_attribute(int vendor
, int tag
) const
358 gold_assert(vendor
>= OBJ_ATTR_FIRST
&& vendor
<= OBJ_ATTR_LAST
);
359 return this->vendor_object_attributes_
[vendor
]->get_attribute(tag
);
362 // Merge target-independent attributes from another Attributes_section_data
363 // of an object called NAME.
365 merge(const char* name
, const Attributes_section_data
* pasd
);
367 // Write to byte stream in an unsigned char vector.
369 write(std::vector
<unsigned char>*) const;
373 static const int OBJ_ATTR_FIRST
= Object_attribute::OBJ_ATTR_FIRST
;
374 static const int OBJ_ATTR_LAST
= Object_attribute::OBJ_ATTR_LAST
;
376 // Vendor object attributes.
377 Vendor_object_attributes
* vendor_object_attributes_
[OBJ_ATTR_LAST
+1];
380 // This class is used for writing out an Attribute_section_data.
382 class Output_attributes_section_data
: public Output_section_data
385 Output_attributes_section_data(const Attributes_section_data
& asd
)
386 : Output_section_data(1), attributes_section_data_(asd
)
390 // Write to a map file.
392 do_print_to_mapfile(Mapfile
* mapfile
) const
393 { mapfile
->print_output_data(this, _("** attributes")); }
395 // Write the data to the output file.
397 do_write(Output_file
*);
399 // Set final data size.
401 set_final_data_size()
402 { this->set_data_size(attributes_section_data_
.size()); }
405 // Attributes_section_data corresponding to this.
406 const Attributes_section_data
& attributes_section_data_
;
409 } // End namespace gold.
411 #endif // !defined(GOLD_ATTRIBUTES_H)