1*a9fa9459Szrj // attributes.cc -- object attributes for gold
2*a9fa9459Szrj 
3*a9fa9459Szrj // Copyright (C) 2009-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Doug Kwan <dougkwan@google.com>.
5*a9fa9459Szrj // This file contains code adapted from BFD.
6*a9fa9459Szrj 
7*a9fa9459Szrj // This file is part of gold.
8*a9fa9459Szrj 
9*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
10*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
11*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
12*a9fa9459Szrj // (at your option) any later version.
13*a9fa9459Szrj 
14*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
15*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
16*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*a9fa9459Szrj // GNU General Public License for more details.
18*a9fa9459Szrj 
19*a9fa9459Szrj // You should have received a copy of the GNU General Public License
20*a9fa9459Szrj // along with this program; if not, write to the Free Software
21*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22*a9fa9459Szrj // MA 02110-1301, USA.
23*a9fa9459Szrj 
24*a9fa9459Szrj #include "gold.h"
25*a9fa9459Szrj 
26*a9fa9459Szrj #include <limits>
27*a9fa9459Szrj 
28*a9fa9459Szrj #include "attributes.h"
29*a9fa9459Szrj #include "elfcpp.h"
30*a9fa9459Szrj #include "target.h"
31*a9fa9459Szrj #include "parameters.h"
32*a9fa9459Szrj #include "int_encoding.h"
33*a9fa9459Szrj 
34*a9fa9459Szrj namespace gold
35*a9fa9459Szrj {
36*a9fa9459Szrj 
37*a9fa9459Szrj // Object_attribute methods.
38*a9fa9459Szrj 
39*a9fa9459Szrj // Return size of attribute encode in ULEB128.
40*a9fa9459Szrj 
41*a9fa9459Szrj size_t
size(int tag) const42*a9fa9459Szrj Object_attribute::size(int tag) const
43*a9fa9459Szrj {
44*a9fa9459Szrj   // Attributes with default values are not written out.
45*a9fa9459Szrj   if (this->is_default_attribute())
46*a9fa9459Szrj     return 0;
47*a9fa9459Szrj 
48*a9fa9459Szrj   size_t size = get_length_as_unsigned_LEB_128(tag);
49*a9fa9459Szrj   if (Object_attribute::attribute_type_has_int_value(this->type_))
50*a9fa9459Szrj     size += get_length_as_unsigned_LEB_128(this->int_value_);
51*a9fa9459Szrj   if (Object_attribute::attribute_type_has_string_value(this->type_))
52*a9fa9459Szrj     size += this->string_value_.size() + 1;
53*a9fa9459Szrj   return size;
54*a9fa9459Szrj }
55*a9fa9459Szrj 
56*a9fa9459Szrj // Whether this has the default value (0/"").
57*a9fa9459Szrj 
58*a9fa9459Szrj bool
is_default_attribute() const59*a9fa9459Szrj Object_attribute::is_default_attribute() const
60*a9fa9459Szrj {
61*a9fa9459Szrj   if (Object_attribute::attribute_type_has_int_value(this->type_)
62*a9fa9459Szrj       && this->int_value_ != 0)
63*a9fa9459Szrj     return false;
64*a9fa9459Szrj   if (Object_attribute::attribute_type_has_string_value(this->type_)
65*a9fa9459Szrj       && !this->string_value_.empty())
66*a9fa9459Szrj     return false;
67*a9fa9459Szrj   if (Object_attribute::attribute_type_has_no_default(this->type_))
68*a9fa9459Szrj     return false;
69*a9fa9459Szrj 
70*a9fa9459Szrj   return true;
71*a9fa9459Szrj }
72*a9fa9459Szrj 
73*a9fa9459Szrj // Whether this matches another Object_attribute OA in merging.
74*a9fa9459Szrj // Two Object_attributes match if they have the same values.
75*a9fa9459Szrj 
76*a9fa9459Szrj bool
matches(const Object_attribute & oa) const77*a9fa9459Szrj Object_attribute::matches(const Object_attribute& oa) const
78*a9fa9459Szrj {
79*a9fa9459Szrj   return ((this->int_value_ != oa.int_value_)
80*a9fa9459Szrj 	  && (this->string_value_ == oa.string_value_));
81*a9fa9459Szrj }
82*a9fa9459Szrj 
83*a9fa9459Szrj // Write this with TAG to a BUFFER.
84*a9fa9459Szrj 
85*a9fa9459Szrj void
write(int tag,std::vector<unsigned char> * buffer) const86*a9fa9459Szrj Object_attribute::write(
87*a9fa9459Szrj     int tag,
88*a9fa9459Szrj     std::vector<unsigned char>* buffer) const
89*a9fa9459Szrj {
90*a9fa9459Szrj   // No need to write default attributes.
91*a9fa9459Szrj   if (this->is_default_attribute())
92*a9fa9459Szrj     return;
93*a9fa9459Szrj 
94*a9fa9459Szrj   // Write tag.
95*a9fa9459Szrj   write_unsigned_LEB_128(buffer, convert_types<uint64_t, int>(tag));
96*a9fa9459Szrj 
97*a9fa9459Szrj   // Write integer value.
98*a9fa9459Szrj   if (Object_attribute::attribute_type_has_int_value(this->type_))
99*a9fa9459Szrj     write_unsigned_LEB_128(buffer,
100*a9fa9459Szrj 			   convert_types<uint64_t, int>(this->int_value_));
101*a9fa9459Szrj 
102*a9fa9459Szrj   // Write string value.
103*a9fa9459Szrj   if (Object_attribute::attribute_type_has_string_value(this->type_))
104*a9fa9459Szrj     {
105*a9fa9459Szrj       const unsigned char* start =
106*a9fa9459Szrj 	reinterpret_cast<const unsigned char*>(this->string_value_.c_str());
107*a9fa9459Szrj       const unsigned char* end = start + this->string_value_.size() + 1;
108*a9fa9459Szrj       buffer->insert(buffer->end(), start, end);
109*a9fa9459Szrj     }
110*a9fa9459Szrj }
111*a9fa9459Szrj 
112*a9fa9459Szrj // Vendor_object_attributes methods.
113*a9fa9459Szrj 
114*a9fa9459Szrj // Copying constructor.
115*a9fa9459Szrj 
Vendor_object_attributes(const Vendor_object_attributes & voa)116*a9fa9459Szrj Vendor_object_attributes::Vendor_object_attributes(
117*a9fa9459Szrj     const Vendor_object_attributes& voa)
118*a9fa9459Szrj {
119*a9fa9459Szrj   this->vendor_ = voa.vendor_;
120*a9fa9459Szrj 
121*a9fa9459Szrj   for (int i = 0; i < NUM_KNOWN_ATTRIBUTES; ++i)
122*a9fa9459Szrj     this->known_attributes_[i] = voa.known_attributes_[i];
123*a9fa9459Szrj 
124*a9fa9459Szrj   // We do not handle attribute deletion.  So this must be empty.
125*a9fa9459Szrj   gold_assert(this->other_attributes_.empty());
126*a9fa9459Szrj 
127*a9fa9459Szrj   for (Other_attributes::const_iterator p = voa.other_attributes_.begin();
128*a9fa9459Szrj        p != voa.other_attributes_.end();
129*a9fa9459Szrj        ++p)
130*a9fa9459Szrj     this->other_attributes_[p->first] = new Object_attribute(*(p->second));
131*a9fa9459Szrj }
132*a9fa9459Szrj 
133*a9fa9459Szrj // Size of this in number of bytes.
134*a9fa9459Szrj 
135*a9fa9459Szrj size_t
size() const136*a9fa9459Szrj Vendor_object_attributes::size() const
137*a9fa9459Szrj {
138*a9fa9459Szrj   if (this->name() == NULL)
139*a9fa9459Szrj     return 0;
140*a9fa9459Szrj 
141*a9fa9459Szrj   size_t data_size = 0;
142*a9fa9459Szrj   for (int i = 4; i < NUM_KNOWN_ATTRIBUTES; ++i)
143*a9fa9459Szrj     data_size += this->known_attributes_[i].size(i);
144*a9fa9459Szrj 
145*a9fa9459Szrj   for (Other_attributes::const_iterator p = this->other_attributes_.begin();
146*a9fa9459Szrj        p != this->other_attributes_.end();
147*a9fa9459Szrj        ++p)
148*a9fa9459Szrj     data_size += p->second->size(p->first);
149*a9fa9459Szrj 
150*a9fa9459Szrj   // <size> <vendor_name> NUL 0x1 <size>
151*a9fa9459Szrj   return ((data_size != 0
152*a9fa9459Szrj 	   || this->vendor_ == Object_attribute::OBJ_ATTR_PROC)
153*a9fa9459Szrj 	  ? data_size + strlen(this->name()) + 2 + 2 * 4
154*a9fa9459Szrj 	  : 0);
155*a9fa9459Szrj }
156*a9fa9459Szrj 
157*a9fa9459Szrj // Return a new attribute associated with TAG.
158*a9fa9459Szrj 
159*a9fa9459Szrj Object_attribute*
new_attribute(int tag)160*a9fa9459Szrj Vendor_object_attributes::new_attribute(int tag)
161*a9fa9459Szrj {
162*a9fa9459Szrj   int type = Object_attribute::arg_type(this->vendor_, tag);
163*a9fa9459Szrj 
164*a9fa9459Szrj   if (tag < NUM_KNOWN_ATTRIBUTES)
165*a9fa9459Szrj     {
166*a9fa9459Szrj       this->known_attributes_[tag].set_type(type);
167*a9fa9459Szrj       return &this->known_attributes_[tag];
168*a9fa9459Szrj     }
169*a9fa9459Szrj   else
170*a9fa9459Szrj     {
171*a9fa9459Szrj       Object_attribute* attr = new Object_attribute();
172*a9fa9459Szrj 
173*a9fa9459Szrj       // This should be the first time we insert this.
174*a9fa9459Szrj       std::pair<Other_attributes::iterator, bool> ins =
175*a9fa9459Szrj 	this->other_attributes_.insert(std::make_pair(tag, attr));
176*a9fa9459Szrj       gold_assert(ins.second);
177*a9fa9459Szrj 
178*a9fa9459Szrj       attr->set_type(type);
179*a9fa9459Szrj       return attr;
180*a9fa9459Szrj     }
181*a9fa9459Szrj }
182*a9fa9459Szrj 
183*a9fa9459Szrj // Return an attribute associated with TAG.
184*a9fa9459Szrj 
185*a9fa9459Szrj Object_attribute*
get_attribute(int tag)186*a9fa9459Szrj Vendor_object_attributes::get_attribute(int tag)
187*a9fa9459Szrj {
188*a9fa9459Szrj   if (tag < NUM_KNOWN_ATTRIBUTES)
189*a9fa9459Szrj     return &this->known_attributes_[tag];
190*a9fa9459Szrj   else
191*a9fa9459Szrj     {
192*a9fa9459Szrj       Other_attributes::iterator p =
193*a9fa9459Szrj 	this->other_attributes_.find(tag);
194*a9fa9459Szrj       return p != this->other_attributes_.end() ? p->second : NULL;
195*a9fa9459Szrj     }
196*a9fa9459Szrj }
197*a9fa9459Szrj 
198*a9fa9459Szrj const Object_attribute*
get_attribute(int tag) const199*a9fa9459Szrj Vendor_object_attributes::get_attribute(int tag) const
200*a9fa9459Szrj {
201*a9fa9459Szrj   if (tag < NUM_KNOWN_ATTRIBUTES)
202*a9fa9459Szrj     return &this->known_attributes_[tag];
203*a9fa9459Szrj   else
204*a9fa9459Szrj     {
205*a9fa9459Szrj       Other_attributes::const_iterator p =
206*a9fa9459Szrj 	this->other_attributes_.find(tag);
207*a9fa9459Szrj       return p != this->other_attributes_.end() ? p->second : NULL;
208*a9fa9459Szrj     }
209*a9fa9459Szrj }
210*a9fa9459Szrj 
211*a9fa9459Szrj // Write attributes to BUFFER.
212*a9fa9459Szrj 
213*a9fa9459Szrj void
write(std::vector<unsigned char> * buffer) const214*a9fa9459Szrj Vendor_object_attributes::write(std::vector<unsigned char>* buffer) const
215*a9fa9459Szrj {
216*a9fa9459Szrj   // Write subsection size.
217*a9fa9459Szrj   size_t voa_size = this->size();
218*a9fa9459Szrj   uint32_t voa_size_as_u32 = convert_types<uint32_t, size_t>(voa_size);
219*a9fa9459Szrj   insert_into_vector<32>(buffer, voa_size_as_u32);
220*a9fa9459Szrj 
221*a9fa9459Szrj   // Write vendor name.
222*a9fa9459Szrj   const unsigned char* vendor_start =
223*a9fa9459Szrj     reinterpret_cast<const unsigned char*>(this->name());
224*a9fa9459Szrj   size_t vendor_length = strlen(this->name()) + 1;
225*a9fa9459Szrj   const unsigned char* vendor_end = vendor_start + vendor_length;
226*a9fa9459Szrj   buffer->insert(buffer->end(), vendor_start, vendor_end);
227*a9fa9459Szrj 
228*a9fa9459Szrj   // Write file tag.
229*a9fa9459Szrj   buffer->push_back(Object_attribute::Tag_File);
230*a9fa9459Szrj 
231*a9fa9459Szrj   // Write attributes size.
232*a9fa9459Szrj   uint32_t attributes_size_as_u32 =
233*a9fa9459Szrj     convert_types<uint32_t, size_t>(voa_size - 4 - vendor_length);
234*a9fa9459Szrj   insert_into_vector<32>(buffer, attributes_size_as_u32);
235*a9fa9459Szrj 
236*a9fa9459Szrj   // Write known attributes, skipping any defaults.
237*a9fa9459Szrj   for (int i = 4; i < NUM_KNOWN_ATTRIBUTES; ++i)
238*a9fa9459Szrj     {
239*a9fa9459Szrj       // A target may write known attributes in a special order.
240*a9fa9459Szrj       // Call target hook to remap tags.  Attributes_order is the identity
241*a9fa9459Szrj       // function if no re-ordering is required.
242*a9fa9459Szrj       int tag = parameters->target().attributes_order(i);
243*a9fa9459Szrj       this->known_attributes_[tag].write(tag, buffer);
244*a9fa9459Szrj     }
245*a9fa9459Szrj 
246*a9fa9459Szrj   // Write other attributes.
247*a9fa9459Szrj   for (Other_attributes::const_iterator q = this->other_attributes_.begin();
248*a9fa9459Szrj        q != this->other_attributes_.end();
249*a9fa9459Szrj        ++q)
250*a9fa9459Szrj     q->second->write(q->first, buffer);
251*a9fa9459Szrj }
252*a9fa9459Szrj 
253*a9fa9459Szrj // Attributes_section_data methods.
254*a9fa9459Szrj 
255*a9fa9459Szrj // Compute encoded size of this.
256*a9fa9459Szrj 
257*a9fa9459Szrj size_t
size() const258*a9fa9459Szrj Attributes_section_data::size() const
259*a9fa9459Szrj {
260*a9fa9459Szrj   size_t data_size = 0;
261*a9fa9459Szrj   for(int vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; ++vendor)
262*a9fa9459Szrj     data_size += this->vendor_object_attributes_[vendor]->size();
263*a9fa9459Szrj 
264*a9fa9459Szrj   // 'A' <sections for each vendor>
265*a9fa9459Szrj   return data_size != 0 ? data_size + 1 : 0;
266*a9fa9459Szrj }
267*a9fa9459Szrj 
268*a9fa9459Szrj // Construct an Attributes_section_data object by parsing section contents
269*a9fa9459Szrj // specified by VIEW and SIZE.
270*a9fa9459Szrj 
Attributes_section_data(const unsigned char * view,section_size_type size)271*a9fa9459Szrj Attributes_section_data::Attributes_section_data(
272*a9fa9459Szrj     const unsigned char* view,
273*a9fa9459Szrj     section_size_type size)
274*a9fa9459Szrj {
275*a9fa9459Szrj   for (int vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; ++vendor)
276*a9fa9459Szrj     this->vendor_object_attributes_[vendor] =
277*a9fa9459Szrj       new Vendor_object_attributes(vendor);
278*a9fa9459Szrj 
279*a9fa9459Szrj   const unsigned char* p = view;
280*a9fa9459Szrj   p = view;
281*a9fa9459Szrj   if (size > 0 && p != NULL && *(p++) == 'A')
282*a9fa9459Szrj     {
283*a9fa9459Szrj       size--;
284*a9fa9459Szrj       while (size > 0)
285*a9fa9459Szrj 	{
286*a9fa9459Szrj 	  // Size of vendor attributes section.
287*a9fa9459Szrj 	  section_size_type section_size =
288*a9fa9459Szrj 	    convert_to_section_size_type(read_from_pointer<32>(&p));
289*a9fa9459Szrj 
290*a9fa9459Szrj 	  if (section_size > size)
291*a9fa9459Szrj 	    section_size = size;
292*a9fa9459Szrj 	  size -= section_size;
293*a9fa9459Szrj 
294*a9fa9459Szrj 	  const char* section_name = reinterpret_cast<const char*>(p);
295*a9fa9459Szrj 	  section_size_type section_name_size = strlen(section_name) + 1;
296*a9fa9459Szrj 	  section_size -= section_name_size + 4;
297*a9fa9459Szrj 
298*a9fa9459Szrj 	  int vendor;
299*a9fa9459Szrj 	  const char* std_section = parameters->target().attributes_vendor();
300*a9fa9459Szrj 	  if (std_section != NULL && strcmp(section_name, std_section) == 0)
301*a9fa9459Szrj 	    vendor = Object_attribute::OBJ_ATTR_PROC;
302*a9fa9459Szrj 	  else if (strcmp(section_name, "gnu") == 0)
303*a9fa9459Szrj 	    vendor = Object_attribute::OBJ_ATTR_GNU;
304*a9fa9459Szrj 	  else
305*a9fa9459Szrj 	    {
306*a9fa9459Szrj 	      // Other vendor section.  Ignore it.
307*a9fa9459Szrj 	      p += section_name_size + section_size;
308*a9fa9459Szrj 	      continue;
309*a9fa9459Szrj 	    }
310*a9fa9459Szrj 	  p += section_name_size;
311*a9fa9459Szrj 
312*a9fa9459Szrj 	  while (section_size > 0)
313*a9fa9459Szrj 	    {
314*a9fa9459Szrj 	      const unsigned char* subsection_start = p;
315*a9fa9459Szrj 
316*a9fa9459Szrj 	      // Read vendor subsection index and size.
317*a9fa9459Szrj 	      size_t uleb128_len;
318*a9fa9459Szrj 	      uint64_t val = read_unsigned_LEB_128(p, &uleb128_len);
319*a9fa9459Szrj 	      p += uleb128_len;
320*a9fa9459Szrj 
321*a9fa9459Szrj 	      int tag = convert_types<int, uint64_t>(val);
322*a9fa9459Szrj 	      section_size_type subsection_size =
323*a9fa9459Szrj 		convert_to_section_size_type(read_from_pointer<32>(&p));
324*a9fa9459Szrj 	      section_size -= subsection_size;
325*a9fa9459Szrj 	      subsection_size -= (p - subsection_start);
326*a9fa9459Szrj 
327*a9fa9459Szrj 	      const unsigned char* end = p + subsection_size;
328*a9fa9459Szrj 	      switch (tag)
329*a9fa9459Szrj 		{
330*a9fa9459Szrj 		case Object_attribute::Tag_File:
331*a9fa9459Szrj 		  while (p < end)
332*a9fa9459Szrj 		    {
333*a9fa9459Szrj 		      val = read_unsigned_LEB_128(p, &uleb128_len);
334*a9fa9459Szrj 		      p += uleb128_len;
335*a9fa9459Szrj 		      tag = convert_types<int, uint64_t>(val);
336*a9fa9459Szrj 		      Vendor_object_attributes* pvoa =
337*a9fa9459Szrj 			this->vendor_object_attributes_[vendor];
338*a9fa9459Szrj 		      Object_attribute* attr = pvoa->new_attribute(tag);
339*a9fa9459Szrj 		      const char* string_arg;
340*a9fa9459Szrj 		      unsigned int int_arg;
341*a9fa9459Szrj 
342*a9fa9459Szrj 		      int type = Object_attribute::arg_type(vendor, tag);
343*a9fa9459Szrj 		      switch (type
344*a9fa9459Szrj 			      & (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
345*a9fa9459Szrj 				 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL))
346*a9fa9459Szrj 			{
347*a9fa9459Szrj 			case (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
348*a9fa9459Szrj 			      | Object_attribute::ATTR_TYPE_FLAG_STR_VAL):
349*a9fa9459Szrj 			  val = read_unsigned_LEB_128(p, &uleb128_len);
350*a9fa9459Szrj 			  p += uleb128_len;
351*a9fa9459Szrj 			  int_arg = convert_types<unsigned int, uint64_t>(val);
352*a9fa9459Szrj 			  string_arg = reinterpret_cast<const char *>(p);
353*a9fa9459Szrj 			  attr->set_int_value(int_arg);
354*a9fa9459Szrj 			  p += strlen(string_arg) + 1;
355*a9fa9459Szrj 			  break;
356*a9fa9459Szrj 			case Object_attribute::ATTR_TYPE_FLAG_STR_VAL:
357*a9fa9459Szrj 			  string_arg = reinterpret_cast<const char *>(p);
358*a9fa9459Szrj 			  attr->set_string_value(string_arg);
359*a9fa9459Szrj 			  p += strlen(string_arg) + 1;
360*a9fa9459Szrj 			  break;
361*a9fa9459Szrj 			case Object_attribute::ATTR_TYPE_FLAG_INT_VAL:
362*a9fa9459Szrj 			  val = read_unsigned_LEB_128(p, &uleb128_len);
363*a9fa9459Szrj 			  p += uleb128_len;
364*a9fa9459Szrj 			  int_arg = convert_types<unsigned int, uint64_t>(val);
365*a9fa9459Szrj 			  attr->set_int_value(int_arg);
366*a9fa9459Szrj 			  break;
367*a9fa9459Szrj 			default:
368*a9fa9459Szrj 			  gold_unreachable();
369*a9fa9459Szrj 			}
370*a9fa9459Szrj 		    }
371*a9fa9459Szrj 		  break;
372*a9fa9459Szrj 		case Object_attribute::Tag_Section:
373*a9fa9459Szrj 		case Object_attribute::Tag_Symbol:
374*a9fa9459Szrj 		  // Don't have anywhere convenient to attach these.
375*a9fa9459Szrj 		  // Fall through for now.
376*a9fa9459Szrj 		default:
377*a9fa9459Szrj 		  // Ignore things we don't know about.
378*a9fa9459Szrj 		  p += subsection_size;
379*a9fa9459Szrj 		  subsection_size = 0;
380*a9fa9459Szrj 		  break;
381*a9fa9459Szrj 		}
382*a9fa9459Szrj 	    }
383*a9fa9459Szrj 	}
384*a9fa9459Szrj     }
385*a9fa9459Szrj }
386*a9fa9459Szrj 
387*a9fa9459Szrj // Merge target-independent attributes from another Attribute_section_data
388*a9fa9459Szrj // ASD from an object called NAME into this.
389*a9fa9459Szrj 
390*a9fa9459Szrj void
merge(const char * name,const Attributes_section_data * pasd)391*a9fa9459Szrj Attributes_section_data::merge(
392*a9fa9459Szrj     const char* name,
393*a9fa9459Szrj     const Attributes_section_data* pasd)
394*a9fa9459Szrj {
395*a9fa9459Szrj   // The only common attribute is currently Tag_compatibility,
396*a9fa9459Szrj   // accepted in both processor and "gnu" sections.
397*a9fa9459Szrj   for (int vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; ++vendor)
398*a9fa9459Szrj     {
399*a9fa9459Szrj       // Handle Tag_compatibility.  The tags are only compatible if the flags
400*a9fa9459Szrj       // are identical and, if the flags are '1', the strings are identical.
401*a9fa9459Szrj       // If the flags are non-zero, then we can only use the string "gnu".
402*a9fa9459Szrj       const Object_attribute* in_attr =
403*a9fa9459Szrj 	&pasd->known_attributes(vendor)[Object_attribute::Tag_compatibility];
404*a9fa9459Szrj       Object_attribute* out_attr =
405*a9fa9459Szrj 	&this->known_attributes(vendor)[Object_attribute::Tag_compatibility];
406*a9fa9459Szrj 
407*a9fa9459Szrj       if (in_attr->int_value() > 0
408*a9fa9459Szrj 	  && in_attr->string_value() != "gnu")
409*a9fa9459Szrj 	{
410*a9fa9459Szrj 	  gold_error(_("%s: must be processed by '%s' toolchain"),
411*a9fa9459Szrj 		     name, in_attr->string_value().c_str());
412*a9fa9459Szrj 	  return;
413*a9fa9459Szrj 	}
414*a9fa9459Szrj 
415*a9fa9459Szrj       if (in_attr->int_value() != out_attr->int_value()
416*a9fa9459Szrj 	  || in_attr->string_value() != out_attr->string_value())
417*a9fa9459Szrj 	{
418*a9fa9459Szrj 	  gold_error(_("%s: object tag '%d, %s' is "
419*a9fa9459Szrj 		       "incompatible with tag '%d, %s'"),
420*a9fa9459Szrj 		     name, in_attr->int_value(),
421*a9fa9459Szrj 		     in_attr->string_value().c_str(),
422*a9fa9459Szrj 		     out_attr->int_value(),
423*a9fa9459Szrj 		     out_attr->string_value().c_str());
424*a9fa9459Szrj 	}
425*a9fa9459Szrj     }
426*a9fa9459Szrj }
427*a9fa9459Szrj 
428*a9fa9459Szrj // Write to a buffer.
429*a9fa9459Szrj 
430*a9fa9459Szrj void
write(std::vector<unsigned char> * buffer) const431*a9fa9459Szrj Attributes_section_data::write(std::vector<unsigned char>* buffer) const
432*a9fa9459Szrj {
433*a9fa9459Szrj   buffer->push_back('A');
434*a9fa9459Szrj   for (int vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; ++vendor)
435*a9fa9459Szrj     if (this->vendor_object_attributes_[vendor]->size() != 0)
436*a9fa9459Szrj       this->vendor_object_attributes_[vendor]->write(buffer);
437*a9fa9459Szrj }
438*a9fa9459Szrj 
439*a9fa9459Szrj // Methods for Output_attributes_section_data.
440*a9fa9459Szrj 
441*a9fa9459Szrj // Write attributes section data to file OF.
442*a9fa9459Szrj 
443*a9fa9459Szrj void
do_write(Output_file * of)444*a9fa9459Szrj Output_attributes_section_data::do_write(Output_file* of)
445*a9fa9459Szrj {
446*a9fa9459Szrj   off_t offset = this->offset();
447*a9fa9459Szrj   const section_size_type oview_size =
448*a9fa9459Szrj     convert_to_section_size_type(this->data_size());
449*a9fa9459Szrj   unsigned char* const oview = of->get_output_view(offset, oview_size);
450*a9fa9459Szrj 
451*a9fa9459Szrj   std::vector<unsigned char> buffer;
452*a9fa9459Szrj   this->attributes_section_data_.write(&buffer);
453*a9fa9459Szrj   gold_assert(convert_to_section_size_type(buffer.size()) == oview_size);
454*a9fa9459Szrj   memcpy(oview, &buffer.front(), buffer.size());
455*a9fa9459Szrj   of->write_output_view(this->offset(), oview_size, oview);
456*a9fa9459Szrj }
457*a9fa9459Szrj 
458*a9fa9459Szrj } // End namespace gold.
459