Blame include/simple-object.h

Packit bbfece
/* simple-object.h -- simple routines to read and write object files
Packit bbfece
   Copyright (C) 2010-2018 Free Software Foundation, Inc.
Packit bbfece
   Written by Ian Lance Taylor, Google.
Packit bbfece
Packit bbfece
This program is free software; you can redistribute it and/or modify it
Packit bbfece
under the terms of the GNU General Public License as published by the
Packit bbfece
Free Software Foundation; either version 2, or (at your option) any
Packit bbfece
later version.
Packit bbfece
Packit bbfece
This program is distributed in the hope that it will be useful,
Packit bbfece
but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bbfece
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit bbfece
GNU General Public License for more details.
Packit bbfece
Packit bbfece
You should have received a copy of the GNU General Public License
Packit bbfece
along with this program; if not, write to the Free Software
Packit bbfece
Foundation, 51 Franklin Street - Fifth Floor,
Packit bbfece
Boston, MA 02110-1301, USA.  */
Packit bbfece
Packit bbfece
#ifndef SIMPLE_OBJECT_H
Packit bbfece
#define SIMPLE_OBJECT_H
Packit bbfece
Packit bbfece
#include <stddef.h>
Packit bbfece
#include <sys/types.h>
Packit bbfece
Packit bbfece
#ifdef HAVE_UNISTD_H
Packit bbfece
#include <unistd.h>
Packit bbfece
#endif
Packit bbfece
Packit bbfece
#ifdef __cplusplus
Packit bbfece
extern "C" {
Packit bbfece
#endif
Packit bbfece
Packit bbfece
/* This header file provides four types with associated functions.
Packit bbfece
   They are used to read and write object files.  This is a minimal
Packit bbfece
   interface, intended to support the needs of gcc without bringing in
Packit bbfece
   all the power and complexity of BFD.  */
Packit bbfece
Packit bbfece
/* The type simple_object_read * is used to read an existing object
Packit bbfece
   file.  */
Packit bbfece
Packit bbfece
typedef struct simple_object_read_struct simple_object_read;
Packit bbfece
Packit bbfece
/* Create an simple_object_read given DESCRIPTOR, an open file
Packit bbfece
   descriptor, and OFFSET, an offset within the file.  The offset is
Packit bbfece
   for use with archives, and should be 0 for an ordinary object file.
Packit bbfece
   The descriptor must remain open until done with the returned
Packit bbfece
   simple_object_read.  SEGMENT_NAME is used on Mach-O and is required
Packit bbfece
   on that platform: it means to only look at sections within the
Packit bbfece
   segment with that name.  It is ignored for other object file
Packit bbfece
   formats.  On error, this function returns NULL, and sets *ERRMSG to
Packit bbfece
   an error string and sets *ERR to an errno value or 0 if there is no
Packit bbfece
   relevant errno.  */
Packit bbfece
Packit bbfece
extern simple_object_read *
Packit bbfece
simple_object_start_read (int descriptor, off_t offset,
Packit bbfece
			  const char *segment_name, const char **errmsg,
Packit bbfece
			  int *err);
Packit bbfece
Packit bbfece
/* Call PFN for each section in SIMPLE_OBJECT, passing it the section
Packit bbfece
   name, offset within the file of the section contents, and length of
Packit bbfece
   the section contents.  The offset within the file is relative to
Packit bbfece
   the offset passed to simple_object_start_read.  The DATA argument
Packit bbfece
   to simple_object_find_sections is passed on to PFN.  If PFN returns
Packit bbfece
   0, the loop is stopped and simple_object_find_sections returns.  If
Packit bbfece
   PFN returns non-zero, the loop continues.  On success this returns
Packit bbfece
   NULL.  On error it returns an error string, and sets *ERR to an
Packit bbfece
   errno value or 0 if there is no relevant errno.  */
Packit bbfece
Packit bbfece
extern const char *
Packit bbfece
simple_object_find_sections (simple_object_read *simple_object,
Packit bbfece
			     int (*pfn) (void *data, const char *,
Packit bbfece
					 off_t offset, off_t length),
Packit bbfece
			     void *data,
Packit bbfece
			     int *err);
Packit bbfece
Packit bbfece
/* Look for the section NAME in SIMPLE_OBJECT.  This returns
Packit bbfece
   information for the first section NAME in SIMPLE_OBJECT.  Note that
Packit bbfece
   calling this multiple times is inefficient; use
Packit bbfece
   simple_object_find_sections instead.
Packit bbfece
Packit bbfece
   If found, return 1 and set *OFFSET to the offset in the file of the
Packit bbfece
   section contents and set *LENGTH to the length of the section
Packit bbfece
   contents.  *OFFSET will be relative to the offset passed to
Packit bbfece
   simple_object_start_read.
Packit bbfece
Packit bbfece
   If the section is not found, and no error occurs, return 0 and set
Packit bbfece
   *ERRMSG to NULL.
Packit bbfece
Packit bbfece
   If an error occurs, return 0, set *ERRMSG to an error message, and
Packit bbfece
   set *ERR to an errno value or 0 if there is no relevant errno.  */
Packit bbfece
Packit bbfece
extern int
Packit bbfece
simple_object_find_section (simple_object_read *simple_object,
Packit bbfece
			    const char *name, off_t *offset, off_t *length,
Packit bbfece
			    const char **errmsg, int *err);
Packit bbfece
Packit bbfece
/* Release all resources associated with SIMPLE_OBJECT.  This does not
Packit bbfece
   close the file descriptor.  */
Packit bbfece
Packit bbfece
extern void
Packit bbfece
simple_object_release_read (simple_object_read *);
Packit bbfece
Packit bbfece
/* The type simple_object_attributes holds the attributes of an object
Packit bbfece
   file that matter for creating a file or ensuring that two files are
Packit bbfece
   compatible.  This is a set of magic numbers.  */
Packit bbfece
Packit bbfece
typedef struct simple_object_attributes_struct simple_object_attributes;
Packit bbfece
Packit bbfece
/* Fetch the attributes of SIMPLE_OBJECT.  This information will
Packit bbfece
   persist until simple_object_attributes_release is called, even if
Packit bbfece
   SIMPLE_OBJECT is closed.  On error this returns NULL, sets *ERRMSG
Packit bbfece
   to an error message, and sets *ERR to an errno value or 0 if there
Packit bbfece
   isn't one.  */
Packit bbfece
Packit bbfece
extern simple_object_attributes *
Packit bbfece
simple_object_fetch_attributes (simple_object_read *simple_object,
Packit bbfece
				const char **errmsg, int *err);
Packit bbfece
Packit bbfece
/* Merge the FROM attributes into TO.  If two objects with these
Packit bbfece
   attributes could be linked together without error, returns NULL.
Packit bbfece
   Otherwise, returns an error message, and sets *ERR to an errno
Packit bbfece
   value or 0 if there isn't one.  */
Packit bbfece
Packit bbfece
extern const char *
Packit bbfece
simple_object_attributes_merge (simple_object_attributes *to,
Packit bbfece
				simple_object_attributes *from,
Packit bbfece
				int *err);
Packit bbfece
Packit bbfece
/* Release all resources associated with ATTRS.  */
Packit bbfece
Packit bbfece
extern void
Packit bbfece
simple_object_release_attributes (simple_object_attributes *attrs);
Packit bbfece
Packit bbfece
/* The type simple_object_write is used to create a new object file.  */
Packit bbfece
Packit bbfece
typedef struct simple_object_write_struct simple_object_write;
Packit bbfece
Packit bbfece
/* Start creating a new object file which is like ATTRS.  You must
Packit bbfece
   fetch attribute information from an existing object file before you
Packit bbfece
   can create a new one.  There is currently no support for creating
Packit bbfece
   an object file de novo.  The segment name is only used on Mach-O,
Packit bbfece
   where it is required.  It means that all sections are created
Packit bbfece
   within that segment.  It is ignored for other object file formats.
Packit bbfece
   On error this function returns NULL, sets *ERRMSG to an error
Packit bbfece
   message, and sets *ERR to an errno value or 0 if there isn't
Packit bbfece
   one.  */
Packit bbfece
Packit bbfece
extern simple_object_write *
Packit bbfece
simple_object_start_write (simple_object_attributes *attrs,
Packit bbfece
			   const char *segment_name,
Packit bbfece
			   const char **errmsg, int *err);
Packit bbfece
Packit bbfece
/* The type simple_object_write_section is a handle for a section
Packit bbfece
   which is being written.  */
Packit bbfece
Packit bbfece
typedef struct simple_object_write_section_struct simple_object_write_section;
Packit bbfece
Packit bbfece
/* Add a section to SIMPLE_OBJECT.  NAME is the name of the new
Packit bbfece
   section.  ALIGN is the required alignment expressed as the number
Packit bbfece
   of required low-order 0 bits (e.g., 2 for alignment to a 32-bit
Packit bbfece
   boundary).  The section is created as containing data, readable,
Packit bbfece
   not writable, not executable, not loaded at runtime.  On error this
Packit bbfece
   returns NULL, sets *ERRMSG to an error message, and sets *ERR to an
Packit bbfece
   errno value or 0 if there isn't one.  */
Packit bbfece
Packit bbfece
extern simple_object_write_section *
Packit bbfece
simple_object_write_create_section (simple_object_write *simple_object,
Packit bbfece
				    const char *name, unsigned int align,
Packit bbfece
				    const char **errmsg, int *err);
Packit bbfece
Packit bbfece
/* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT.  If COPY is
Packit bbfece
   non-zero, the data will be copied into memory if necessary.  If
Packit bbfece
   COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released.
Packit bbfece
   On success this returns NULL.  On error this returns an error
Packit bbfece
   message, and sets *ERR to an errno value or 0 if there isn't
Packit bbfece
   one.  */
Packit bbfece
Packit bbfece
extern const char *
Packit bbfece
simple_object_write_add_data (simple_object_write *simple_object,
Packit bbfece
			      simple_object_write_section *section,
Packit bbfece
			      const void *buffer, size_t size,
Packit bbfece
			      int copy, int *err);
Packit bbfece
Packit bbfece
/* Write the complete object file to DESCRIPTOR, an open file
Packit bbfece
   descriptor.  This returns NULL on success.  On error this returns
Packit bbfece
   an error message, and sets *ERR to an errno value or 0 if there
Packit bbfece
   isn't one.  */
Packit bbfece
Packit bbfece
extern const char *
Packit bbfece
simple_object_write_to_file (simple_object_write *simple_object,
Packit bbfece
			     int descriptor, int *err);
Packit bbfece
Packit bbfece
/* Release all resources associated with SIMPLE_OBJECT, including any
Packit bbfece
   simple_object_write_section's that may have been created.  */
Packit bbfece
Packit bbfece
extern void
Packit bbfece
simple_object_release_write (simple_object_write *);
Packit bbfece
Packit bbfece
/* Copy LTO debug sections from SRC_OBJECT to DEST.
Packit bbfece
   If an error occurs, return the errno value in ERR and an error string.  */
Packit bbfece
Packit bbfece
extern const char *
Packit bbfece
simple_object_copy_lto_debug_sections (simple_object_read *src_object,
Packit bbfece
				       const char *dest,
Packit bbfece
				       int *err);
Packit bbfece
Packit bbfece
#ifdef __cplusplus
Packit bbfece
}
Packit bbfece
#endif
Packit bbfece
Packit bbfece
#endif