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.
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
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.
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. */
21 #include "libiberty.h"
22 #include "simple-object.h"
38 #ifdef HAVE_INTTYPES_H
46 #include "simple-object-common.h"
48 /* The known object file formats. */
50 static const struct simple_object_functions
* const format_functions
[] =
52 &simple_object_elf_functions
,
53 &simple_object_mach_o_functions
,
54 &simple_object_coff_functions
57 /* Read data from a file using the simple_object error reporting
61 simple_object_internal_read (int descriptor
, off_t offset
,
62 unsigned char *buffer
, size_t size
,
63 const char **errmsg
, int *err
)
67 if (lseek (descriptor
, offset
, SEEK_SET
) < 0)
74 got
= read (descriptor
, buffer
, size
);
82 if ((size_t) got
< size
)
84 *errmsg
= "file too short";
92 /* Write data to a file using the simple_object error reporting
96 simple_object_internal_write (int descriptor
, off_t offset
,
97 const unsigned char *buffer
, size_t size
,
98 const char **errmsg
, int *err
)
102 if (lseek (descriptor
, offset
, SEEK_SET
) < 0)
109 wrote
= write (descriptor
, buffer
, size
);
117 if ((size_t) wrote
< size
)
119 *errmsg
= "short write";
130 simple_object_start_read (int descriptor
, off_t offset
,
131 const char *segment_name
, const char **errmsg
,
134 unsigned char header
[SIMPLE_OBJECT_MATCH_HEADER_LEN
];
137 if (!simple_object_internal_read (descriptor
, offset
, header
,
138 SIMPLE_OBJECT_MATCH_HEADER_LEN
,
142 len
= sizeof (format_functions
) / sizeof (format_functions
[0]);
143 for (i
= 0; i
< len
; ++i
)
147 data
= format_functions
[i
]->match (header
, descriptor
, offset
,
148 segment_name
, errmsg
, err
);
151 simple_object_read
*ret
;
153 ret
= XNEW (simple_object_read
);
154 ret
->descriptor
= descriptor
;
155 ret
->offset
= offset
;
156 ret
->functions
= format_functions
[i
];
162 *errmsg
= "file not recognized";
167 /* Find all sections. */
170 simple_object_find_sections (simple_object_read
*sobj
,
171 int (*pfn
) (void *, const char *, off_t
, off_t
),
175 return sobj
->functions
->find_sections (sobj
, pfn
, data
, err
);
178 /* Internal data passed to find_one_section. */
180 struct find_one_section_data
182 /* The section we are looking for. */
184 /* Where to store the section offset. */
186 /* Where to store the section length. */
188 /* Set if the name is found. */
192 /* Internal function passed to find_sections. */
195 find_one_section (void *data
, const char *name
, off_t offset
, off_t length
)
197 struct find_one_section_data
*fosd
= (struct find_one_section_data
*) data
;
199 if (strcmp (name
, fosd
->name
) != 0)
202 *fosd
->offset
= offset
;
203 *fosd
->length
= length
;
206 /* Stop iteration. */
210 /* Find a section. */
213 simple_object_find_section (simple_object_read
*sobj
, const char *name
,
214 off_t
*offset
, off_t
*length
,
215 const char **errmsg
, int *err
)
217 struct find_one_section_data fosd
;
220 fosd
.offset
= offset
;
221 fosd
.length
= length
;
224 *errmsg
= simple_object_find_sections (sobj
, find_one_section
,
225 (void *) &fosd
, err
);
233 /* Fetch attributes. */
235 simple_object_attributes
*
236 simple_object_fetch_attributes (simple_object_read
*sobj
, const char **errmsg
,
240 simple_object_attributes
*ret
;
242 data
= sobj
->functions
->fetch_attributes (sobj
, errmsg
, err
);
245 ret
= XNEW (simple_object_attributes
);
246 ret
->functions
= sobj
->functions
;
251 /* Release an simple_object_read. */
254 simple_object_release_read (simple_object_read
*sobj
)
256 sobj
->functions
->release_read (sobj
->data
);
260 /* Merge attributes. */
263 simple_object_attributes_merge (simple_object_attributes
*to
,
264 simple_object_attributes
*from
,
267 if (to
->functions
!= from
->functions
)
270 return "different object file format";
272 return to
->functions
->attributes_merge (to
->data
, from
->data
, err
);
275 /* Release an attributes structure. */
278 simple_object_release_attributes (simple_object_attributes
*attrs
)
280 attrs
->functions
->release_attributes (attrs
->data
);
284 /* Start creating an object file. */
286 simple_object_write
*
287 simple_object_start_write (simple_object_attributes
*attrs
,
288 const char *segment_name
, const char **errmsg
,
292 simple_object_write
*ret
;
294 data
= attrs
->functions
->start_write (attrs
->data
, errmsg
, err
);
297 ret
= XNEW (simple_object_write
);
298 ret
->functions
= attrs
->functions
;
299 ret
->segment_name
= xstrdup (segment_name
);
300 ret
->sections
= NULL
;
301 ret
->last_section
= NULL
;
306 /* Start creating a section. */
308 simple_object_write_section
*
309 simple_object_write_create_section (simple_object_write
*sobj
, const char *name
,
311 const char **errmsg ATTRIBUTE_UNUSED
,
312 int *err ATTRIBUTE_UNUSED
)
314 simple_object_write_section
*ret
;
316 ret
= XNEW (simple_object_write_section
);
318 ret
->name
= xstrdup (name
);
321 ret
->last_buffer
= NULL
;
323 if (sobj
->last_section
== NULL
)
325 sobj
->sections
= ret
;
326 sobj
->last_section
= ret
;
330 sobj
->last_section
->next
= ret
;
331 sobj
->last_section
= ret
;
337 /* Add data to a section. */
340 simple_object_write_add_data (simple_object_write
*sobj ATTRIBUTE_UNUSED
,
341 simple_object_write_section
*section
,
343 size_t size
, int copy
,
344 int *err ATTRIBUTE_UNUSED
)
346 struct simple_object_write_section_buffer
*wsb
;
348 wsb
= XNEW (struct simple_object_write_section_buffer
);
354 wsb
->buffer
= buffer
;
355 wsb
->free_buffer
= NULL
;
359 wsb
->free_buffer
= (void *) XNEWVEC (char, size
);
360 memcpy (wsb
->free_buffer
, buffer
, size
);
361 wsb
->buffer
= wsb
->free_buffer
;
364 if (section
->last_buffer
== NULL
)
366 section
->buffers
= wsb
;
367 section
->last_buffer
= wsb
;
371 section
->last_buffer
->next
= wsb
;
372 section
->last_buffer
= wsb
;
378 /* Write the complete object file. */
381 simple_object_write_to_file (simple_object_write
*sobj
, int descriptor
,
384 return sobj
->functions
->write_to_file (sobj
, descriptor
, err
);
387 /* Release an simple_object_write. */
390 simple_object_release_write (simple_object_write
*sobj
)
392 simple_object_write_section
*section
;
394 free (sobj
->segment_name
);
396 section
= sobj
->sections
;
397 while (section
!= NULL
)
399 struct simple_object_write_section_buffer
*buffer
;
400 simple_object_write_section
*next_section
;
402 buffer
= section
->buffers
;
403 while (buffer
!= NULL
)
405 struct simple_object_write_section_buffer
*next_buffer
;
407 if (buffer
->free_buffer
!= NULL
)
408 XDELETEVEC (buffer
->free_buffer
);
409 next_buffer
= buffer
->next
;
411 buffer
= next_buffer
;
414 next_section
= section
->next
;
415 free (section
->name
);
417 section
= next_section
;
420 sobj
->functions
->release_write (sobj
->data
);