2014-03-28 Richard Biener <rguenther@suse.de>
[platform/upstream/binutils.git] / libiberty / simple-object.c
1 /* simple-object.c -- simple routines to read and write object files.
2    Copyright 2010 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Google.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
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
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA.  */
19
20 #include "config.h"
21 #include "libiberty.h"
22 #include "simple-object.h"
23
24 #include <errno.h>
25
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29
30 #ifdef HAVE_STDINT_H
31 #include <stdint.h>
32 #endif
33
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37
38 #ifdef HAVE_INTTYPES_H
39 #include <inttypes.h>
40 #endif
41
42 #ifndef SEEK_SET
43 #define SEEK_SET 0
44 #endif
45
46 #include "simple-object-common.h"
47
48 /* The known object file formats.  */
49
50 static const struct simple_object_functions * const format_functions[] =
51 {
52   &simple_object_elf_functions,
53   &simple_object_mach_o_functions,
54   &simple_object_coff_functions,
55   &simple_object_xcoff_functions
56 };
57
58 /* Read data from a file using the simple_object error reporting
59    conventions.  */
60
61 int
62 simple_object_internal_read (int descriptor, off_t offset,
63                              unsigned char *buffer, size_t size,
64                              const char **errmsg, int *err)
65 {
66   if (lseek (descriptor, offset, SEEK_SET) < 0)
67     {
68       *errmsg = "lseek";
69       *err = errno;
70       return 0;
71     }
72
73   do
74     {
75       ssize_t got = read (descriptor, buffer, size);
76       if (got == 0)
77         break;
78       else if (got > 0)
79         {
80           buffer += got;
81           size -= got;
82         }
83       else if (errno != EINTR)
84         {
85           *errmsg = "read";
86           *err = errno;
87           return 0;
88         }
89     }
90   while (size > 0);
91
92   if (size > 0)
93     {
94       *errmsg = "file too short";
95       *err = 0;
96       return 0;
97     }
98
99   return 1;
100 }
101
102 /* Write data to a file using the simple_object error reporting
103    conventions.  */
104
105 int
106 simple_object_internal_write (int descriptor, off_t offset,
107                               const unsigned char *buffer, size_t size,
108                               const char **errmsg, int *err)
109 {
110   ssize_t wrote;
111
112   if (lseek (descriptor, offset, SEEK_SET) < 0)
113     {
114       *errmsg = "lseek";
115       *err = errno;
116       return 0;
117     }
118
119   wrote = write (descriptor, buffer, size);
120   if (wrote < 0)
121     {
122       *errmsg = "write";
123       *err = errno;
124       return 0;
125     }
126
127   if ((size_t) wrote < size)
128     {
129       *errmsg = "short write";
130       *err = 0;
131       return 0;
132     }
133
134   return 1;
135 }
136
137 /* Open for read.  */
138
139 simple_object_read *
140 simple_object_start_read (int descriptor, off_t offset,
141                           const char *segment_name, const char **errmsg,
142                           int *err)
143 {
144   unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
145   size_t len, i;
146
147   if (!simple_object_internal_read (descriptor, offset, header,
148                                     SIMPLE_OBJECT_MATCH_HEADER_LEN,
149                                     errmsg, err))
150     return NULL;
151
152   len = sizeof (format_functions) / sizeof (format_functions[0]);
153   for (i = 0; i < len; ++i)
154     {
155       void *data;
156
157       data = format_functions[i]->match (header, descriptor, offset,
158                                          segment_name, errmsg, err);
159       if (data != NULL)
160         {
161           simple_object_read *ret;
162
163           ret = XNEW (simple_object_read);
164           ret->descriptor = descriptor;
165           ret->offset = offset;
166           ret->functions = format_functions[i];
167           ret->data = data;
168           return ret;
169         }
170     }
171
172   *errmsg = "file not recognized";
173   *err = 0;
174   return NULL;
175 }
176
177 /* Find all sections.  */
178
179 const char *
180 simple_object_find_sections (simple_object_read *sobj,
181                              int (*pfn) (void *, const char *, off_t, off_t),
182                              void *data,
183                              int *err)
184 {
185   return sobj->functions->find_sections (sobj, pfn, data, err);
186 }
187
188 /* Internal data passed to find_one_section.  */
189
190 struct find_one_section_data
191 {
192   /* The section we are looking for.  */
193   const char *name;
194   /* Where to store the section offset.  */
195   off_t *offset;
196   /* Where to store the section length.  */
197   off_t *length;
198   /* Set if the name is found.  */
199   int found;
200 };
201
202 /* Internal function passed to find_sections.  */
203
204 static int
205 find_one_section (void *data, const char *name, off_t offset, off_t length)
206 {
207   struct find_one_section_data *fosd = (struct find_one_section_data *) data;
208
209   if (strcmp (name, fosd->name) != 0)
210     return 1;
211
212   *fosd->offset = offset;
213   *fosd->length = length;
214   fosd->found = 1;
215
216   /* Stop iteration.  */
217   return 0;
218 }
219
220 /* Find a section.  */
221
222 int
223 simple_object_find_section (simple_object_read *sobj, const char *name,
224                             off_t *offset, off_t *length,
225                             const char **errmsg, int *err)
226 {
227   struct find_one_section_data fosd;
228
229   fosd.name = name;
230   fosd.offset = offset;
231   fosd.length = length;
232   fosd.found = 0;
233
234   *errmsg = simple_object_find_sections (sobj, find_one_section,
235                                          (void *) &fosd, err);
236   if (*errmsg != NULL)
237     return 0;
238   if (!fosd.found)
239     return 0;
240   return 1;
241 }
242
243 /* Fetch attributes.  */
244
245 simple_object_attributes *
246 simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
247                                 int *err)
248 {
249   void *data;
250   simple_object_attributes *ret;
251
252   data = sobj->functions->fetch_attributes (sobj, errmsg, err);
253   if (data == NULL)
254     return NULL;
255   ret = XNEW (simple_object_attributes);
256   ret->functions = sobj->functions;
257   ret->data = data;
258   return ret;
259 }
260
261 /* Release an simple_object_read.  */
262
263 void
264 simple_object_release_read (simple_object_read *sobj)
265 {
266   sobj->functions->release_read (sobj->data);
267   XDELETE (sobj);
268 }
269
270 /* Merge attributes.  */
271
272 const char *
273 simple_object_attributes_merge (simple_object_attributes *to,
274                                 simple_object_attributes *from,
275                                 int *err)
276 {
277   if (to->functions != from->functions)
278     {
279       *err = 0;
280       return "different object file format";
281     }
282   return to->functions->attributes_merge (to->data, from->data, err);
283 }
284
285 /* Release an attributes structure.  */
286
287 void
288 simple_object_release_attributes (simple_object_attributes *attrs)
289 {
290   attrs->functions->release_attributes (attrs->data);
291   XDELETE (attrs);
292 }
293
294 /* Start creating an object file.  */
295
296 simple_object_write *
297 simple_object_start_write (simple_object_attributes *attrs,
298                            const char *segment_name, const char **errmsg,
299                            int *err)
300 {
301   void *data;
302   simple_object_write *ret;
303
304   data = attrs->functions->start_write (attrs->data, errmsg, err);
305   if (data == NULL)
306     return NULL;
307   ret = XNEW (simple_object_write);
308   ret->functions = attrs->functions;
309   ret->segment_name = xstrdup (segment_name);
310   ret->sections = NULL;
311   ret->last_section = NULL;
312   ret->data = data;
313   return ret;
314 }
315
316 /* Start creating a section.  */
317
318 simple_object_write_section *
319 simple_object_write_create_section (simple_object_write *sobj, const char *name,
320                                     unsigned int align,
321                                     const char **errmsg ATTRIBUTE_UNUSED,
322                                     int *err ATTRIBUTE_UNUSED)
323 {
324   simple_object_write_section *ret;
325
326   ret = XNEW (simple_object_write_section);
327   ret->next = NULL;
328   ret->name = xstrdup (name);
329   ret->align = align;
330   ret->buffers = NULL;
331   ret->last_buffer = NULL;
332
333   if (sobj->last_section == NULL)
334     {
335       sobj->sections = ret;
336       sobj->last_section = ret;
337     }
338   else
339     {
340       sobj->last_section->next = ret;
341       sobj->last_section = ret;
342     }
343
344   return ret;
345 }
346
347 /* Add data to a section.  */
348
349 const char *
350 simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
351                               simple_object_write_section *section,
352                               const void *buffer,
353                               size_t size, int copy,
354                               int *err ATTRIBUTE_UNUSED)
355 {
356   struct simple_object_write_section_buffer *wsb;
357
358   wsb = XNEW (struct simple_object_write_section_buffer);
359   wsb->next = NULL;
360   wsb->size = size;
361
362   if (!copy)
363     {
364       wsb->buffer = buffer;
365       wsb->free_buffer = NULL;
366     }
367   else
368     {
369       wsb->free_buffer = (void *) XNEWVEC (char, size);
370       memcpy (wsb->free_buffer, buffer, size);
371       wsb->buffer = wsb->free_buffer;
372     }
373
374   if (section->last_buffer == NULL)
375     {
376       section->buffers = wsb;
377       section->last_buffer = wsb;
378     }
379   else
380     {
381       section->last_buffer->next = wsb;
382       section->last_buffer = wsb;
383     }
384
385   return NULL;
386 }
387
388 /* Write the complete object file.  */
389
390 const char *
391 simple_object_write_to_file (simple_object_write *sobj, int descriptor,
392                              int *err)
393 {
394   return sobj->functions->write_to_file (sobj, descriptor, err);
395 }
396
397 /* Release an simple_object_write.  */
398
399 void
400 simple_object_release_write (simple_object_write *sobj)
401 {
402   simple_object_write_section *section;
403
404   free (sobj->segment_name);
405
406   section = sobj->sections;
407   while (section != NULL)
408     {
409       struct simple_object_write_section_buffer *buffer;
410       simple_object_write_section *next_section;
411
412       buffer = section->buffers;
413       while (buffer != NULL)
414         {
415           struct simple_object_write_section_buffer *next_buffer;
416
417           if (buffer->free_buffer != NULL)
418             XDELETEVEC (buffer->free_buffer);
419           next_buffer = buffer->next;
420           XDELETE (buffer);
421           buffer = next_buffer;
422         }
423
424       next_section = section->next;
425       free (section->name);
426       XDELETE (section);
427       section = next_section;
428     }
429
430   sobj->functions->release_write (sobj->data);
431   XDELETE (sobj);
432 }