gcc/libiberty/simple-object-common.h

363 lines
10 KiB
C
Raw Normal View History

/* simple-object-common.h -- common structs for object file manipulation.
Copyright (C) 2010-2018 Free Software Foundation, Inc.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
Libiberty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
/* Forward reference. */
struct simple_object_functions;
/* An object file opened for reading. */
struct simple_object_read_struct
{
/* The file descriptor. */
int descriptor;
/* The offset within the file. */
off_t offset;
/* The functions which do the actual work. */
const struct simple_object_functions *functions;
/* Private data for the object file format. */
void *data;
};
/* Object file attributes. */
struct simple_object_attributes_struct
{
/* The functions which do the actual work. */
const struct simple_object_functions *functions;
/* Private data for the object file format. */
void *data;
};
/* An object file being created. */
struct simple_object_write_struct
{
/* The functions which do the actual work. */
const struct simple_object_functions *functions;
/* The segment_name argument from the user. */
char *segment_name;
/* The start of the list of sections. */
simple_object_write_section *sections;
/* The last entry in the list of sections. */
simple_object_write_section *last_section;
/* Private data for the object file format. */
void *data;
};
/* A section in an object file being created. */
struct simple_object_write_section_struct
{
/* Next in the list of sections attached to an
simple_object_write. */
simple_object_write_section *next;
/* The name of this section. */
char *name;
/* The required alignment. */
unsigned int align;
/* The first data attached to this section. */
struct simple_object_write_section_buffer *buffers;
/* The last data attached to this section. */
struct simple_object_write_section_buffer *last_buffer;
};
/* Data attached to a section. */
struct simple_object_write_section_buffer
{
/* The next data for this section. */
struct simple_object_write_section_buffer *next;
/* The size of the buffer. */
size_t size;
/* The actual bytes. */
const void *buffer;
/* A buffer to free, or NULL. */
void *free_buffer;
};
/* The number of bytes we read from the start of the file to pass to
the match function. */
#define SIMPLE_OBJECT_MATCH_HEADER_LEN (16)
/* Format-specific object file functions. */
struct simple_object_functions
{
/* If this file matches these functions, return a new value for the
private data for an simple_object_read. HEADER is the first 16
bytes of the file. DESCRIPTOR, OFFSET, SEGMENT_NAME, ERRMSG, and
ERR are as for simple_object_open_read. If this file does not
match, this function should return NULL with *ERRMSG set to
NULL. */
void *(*match) (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
int descriptor, off_t offset, const char *segment_name,
const char **errmsg, int *err);
/* Implement simple_object_find_sections. */
const char *(*find_sections) (simple_object_read *,
int (*pfn) (void *, const char *,
off_t offset, off_t length),
void *data,
int *err);
/* Return the private data for the attributes for SOBJ. */
void *(*fetch_attributes) (simple_object_read *sobj, const char **errmsg,
int *err);
/* Release the private data for an simple_object_read. */
void (*release_read) (void *);
/* Merge the private data for the attributes of two files. If they
could be linked together, return NULL. Otherwise return an error
message. */
const char *(*attributes_merge) (void *, void *, int *err);
/* Release the private data for an simple_object_attributes. */
void (*release_attributes) (void *);
/* Start creating an object file. */
void *(*start_write) (void *attributes_data, const char **errmsg,
int *err);
/* Write the complete object file. */
const char *(*write_to_file) (simple_object_write *sobj, int descriptor,
int *err);
/* Release the private data for an simple_object_write. */
void (*release_write) (void *);
re PR go/78628 (GO fails to build a translation unit decl) 2017-08-21 Richard Biener <rguenther@suse.de> include/ * simple-object.h (simple_object_copy_lto_debug_sections): New function. libiberty/ * simple-object-common.h (struct simple_object_functions): Add copy_lto_debug_sections hook. * simple-object.c: Include fcntl.h. (handle_lto_debug_sections): New helper function. (simple_object_copy_lto_debug_sections): New function copying early LTO debug sections to regular debug sections in a new file. (simple_object_start_write): Handle NULL segment_name. * simple-object-coff.c (simple_object_coff_functions): Adjust for not implemented copy_lto_debug_sections hook. * simple-object-mach-o.c (simple_object_mach_o_functions): Likewise. * simple-object-xcoff.c (simple_object_xcoff_functions): Likewise. * simple-object-elf.c (SHT_NULL, SHT_SYMTAB, SHT_RELA, SHT_REL, SHT_GROUP): Add various sectopn header types. (SHF_EXCLUDE): Add flag. (Elf32_External_Sym, Elf64_External_Sym): Add symbol struct. (ELF_ST_BIND, ELF_ST_TYPE, ELF_ST_INFO): Add accessors. (STT_OBJECT, STT_FUNC, STT_TLS, STT_GNU_IFUNC): Add Symbol types. (STV_DEFAULT): Add symbol visibility. (SHN_COMMON): Add special section index name. (struct simple_object_elf_write): New. (simple_object_elf_start_write): Adjust for new private data. (simple_object_elf_write_shdr): Pass in values for all fields we write. (simple_object_elf_write_to_file): Adjust. Copy from recorded section headers if requested. (simple_object_elf_release_write): Release private data. (simple_object_elf_copy_lto_debug_sections): Copy and rename sections as denoted by PFN and all their dependences, symbols and relocations to the empty destination file. (simple_object_elf_functions): Adjust for copy_lto_debug_sections hook. gcc/ * debug.h (struct gcc_debug_hooks): Add die_ref_for_decl and register_external_die hooks. (debug_false_tree_charstarstar_uhwistar): Declare. (debug_nothing_tree_charstar_uhwi): Likewise. * debug.c (do_nothing_debug_hooks): Adjust. (debug_false_tree_charstarstar_uhwistar): New do nothing. (debug_nothing_tree_charstar_uhwi): Likewise. * dbxout.c (dbx_debug_hooks): Adjust. (xcoff_debug_hooks): Likewise. * sdbout.c (sdb_debug_hooks): Likewise. * vmsdbgout.c (vmsdbg_debug_hooks): Likewise. * dwarf2out.c (macinfo_label_base): New global. (dwarf2out_register_external_die): New function for the register_external_die hook. (dwarf2out_die_ref_for_decl): Likewise for die_ref_for_decl. (dwarf2_debug_hooks): Use them. (dwarf2_lineno_debug_hooks): Adjust. (struct die_struct): Add with_offset flag. (DEBUG_LTO_DWO_INFO_SECTION, DEBUG_LTO_INFO_SECTION, DEBUG_LTO_DWO_ABBREV_SECTION, DEBUG_LTO_ABBREV_SECTION, DEBUG_LTO_DWO_MACINFO_SECTION, DEBUG_LTO_MACINFO_SECTION, DEBUG_LTO_DWO_MACRO_SECTION, DEBUG_LTO_MACRO_SECTION, DEBUG_LTO_LINE_SECTION, DEBUG_LTO_DWO_STR_OFFSETS_SECTION, DEBUG_LTO_STR_DWO_SECTION, DEBUG_STR_LTO_SECTION): New macros defining section names for the early LTO debug variants. (reset_indirect_string): New helper. (add_AT_external_die_ref): Helper for dwarf2out_register_external_die. (print_dw_val): Add support for offsetted symbol references. (get_ultimate_context): Split out from is_cxx. (is_cxx): Use get_ultimate_context. (is_fortran): Add decl overload. (compute_comp_unit_symbol): Split out worker from compute_section_prefix. (compute_section_prefix): Call compute_comp_unit_symbol and set comdat_type_p here. (output_die): Skip DIE symbol output for the LTO added one. Handle DIE symbol references with offset. (output_comp_unit): Guard section name mangling properly. For LTO debug sections emit a symbol at the section beginning which we use to refer to its DIEs. (add_abstract_origin_attribute): For DIEs registered via dwarf2out_register_external_die directly refer to the early DIE rather than indirectly through the shadow one we created. Remove obsolete call to dwarf2out_abstract_function for non-function/block origins. (gen_array_type_die): When generating early LTO debug do not emit DW_AT_string_length. (gen_formal_parameter_die): Do not re-create DIEs for PARM_DECLs late when in LTO. As suggested place a gcc_unreachable for the DECL_ABSTRACT_P case. (gen_subprogram_die): Avoid another specification DIE for early built declarations/definitions for the late LTO case. (gen_variable_die): Add type references for late duplicated VLA dies when in late LTO. (gen_inlined_subroutine_die): Do not call dwarf2out_abstract_function, we have the abstract instance already. (process_scope_var): Adjust decl DIE contexts in LTO which first puts them in limbo. (gen_decl_die): Do not generate type DIEs late apart from types for VLAs or for decls we do not yet have a DIE. Do not call dwarf2out_abstract_function late. (dwarf2out_early_global_decl): Make sure to create DIEs for abstract instances of a decl first. (dwarf2out_late_global_decl): Adjust comment. (output_macinfo_op): With multiple macro sections use macinfo_label_base to distinguish labels. (output_macinfo): Likewise. Update macinfo_label_base. Pass in the line info label. (note_variable_value_in_expr): When generating LTO resolve all variable values here by generating DIEs as needed. (init_sections_and_labels): Add early LTO debug flag parameter and generate different sections and names if set. Add generation counter for the labels so we can have multiple of them. (reset_dies): Helper to allow DIEs to be output multiple times. (dwarf2out_finish): When outputting DIEs to the fat part of an LTO object first reset DIEs. (dwarf2out_early_finish): Output early DIEs when generating LTO. (modified_type_die): Check for decl_ultimate_origin being self before recursing. (gen_type_die_with_usage): Likewise. (gen_typedef_die): Allow decl_ultimate_origin being self. (set_decl_abstract_flags): Remove. (set_block_abstract_flags): Likewise. (dwarf2out_abstract_function): Treat the early generated DIEs as the abstract copy and only add DW_AT_inline and DW_AT_artificial here and call set_decl_origin_self. If the DIE has an abstract origin don't do anything. * tree.c (free_lang_data): Build a dummy TRANSLATION_UNIT_DECL if we have none yet (Go fails to build one, PR78628). (variably_modified_type_p): Prevent endless recursion for Ada cyclic pointer types. * lto-streamer-in.c: Include debug.h. (dref_queue): New global. (lto_read_tree_1): Stream in DIE references. (lto_input_tree): Register DIE references. (input_function): Stream DECL_DEBUG_ARGS. * lto-streamer-out.c: Include debug.h. (lto_write_tree_1): Output DIE references. (DFS::DFS_write_tree_body): Follow DECL_ABSTRACT_ORIGIN. Force a TRANSLATION_UNIT_DECL DECL_CONTEXT for file-scope decls. (output_function): Stream DECL_DEBUG_ARGS. * tree-streamer-in.c (lto_input_ts_decl_common_tree_pointers): Stream DECL_ABSTRACT_ORIGIN. * tree-streamer-out.c (write_ts_decl_common_tree_pointers): Likewise. (write_ts_decl_minimal_tree_pointers): Force a TRANSLATION_UNIT_DECL DECL_CONTEXT for file-scope decls. * lto-streamer.h (struct dref_entry): Declare. (dref_queue): Likewise. * cfgexpand.c (pass_expand::execute): Do not call the outlining_inline_function hook here. * lto-wrapper.c (debug_obj): New global. (tool_cleanup): Unlink it if required. (debug_objcopy): New function. (run_gcc): Handle early debug sections in the IL files by extracting them to separate files, partially linkin them and feeding the result back as result to the linker. * config/darwin.h (DEBUG_LTO_INFO_SECTION, DEBUG_LTO_ABBREV_SECTION, DEBUG_LTO_MACINFO_SECTION, DEBUG_LTO_LINE_SECTION, DEBUG_STR_LTO_SECTION, DEBUG_LTO_MACRO_SECTION): Put early debug sections into a separate segment. * config/darwin.c (darwin_asm_named_section): Handle __GNU_DWARF_LTO segments. (darwin_asm_dwarf_section): Likewise. (darwin_asm_output_dwarf_offset): Likewise. * config/i386/i386.c (make_resolver_func): Set DECL_IGNORED_P. lto/ * lto.c (unify_scc): Truncate DIE reference queue for dropped SCCs. (lto_read_decls): Process TRANSLATION_UNIT_DECLs. Remove TYPE_DECL debug processing, register DIE references from prevailing SCCs with the debug machinery. (lto_section_with_id): Handle LTO debug sections. libstdc++/ * testsuite/libstdc++-prettyprinters/prettyprinters.exp: Run all tests with -flto as well if supported. testsuite/ * c-c++-common/asan/global-overflow-1.c: Adjust diagnostic location regex to handle the LTO case. * c-c++-common/asan/heap-overflow-1.c: Likewise. * c-c++-common/asan/misalign-1.c: Likewise. * c-c++-common/asan/misalign-2.c: Likewise. * c-c++-common/asan/null-deref-1.c: Likewise. * c-c++-common/asan/stack-overflow-1.c: Likewise. * c-c++-common/asan/strncpy-overflow-1.c: Likewise. * c-c++-common/asan/use-after-free-1.c: Likewise. * c-c++-common/asan/alloca_big_alignment.c: Likewise. * c-c++-common/asan/alloca_detect_custom_size.c: Likewise. * c-c++-common/asan/alloca_overflow_partial.c: Likewise. * c-c++-common/asan/alloca_overflow_right.c: Likewise. * c-c++-common/asan/alloca_underflow_left.c: Likewise. * g++.dg/asan/large-func-test-1.C: Likewise. * gfortran.dg/save_6.f90: Add -flto -g variant of save_5.f90. From-SVN: r251220
2017-08-21 12:29:00 +02:00
/* Copy LTO debug sections. */
const char *(*copy_lto_debug_sections) (simple_object_read *sobj,
simple_object_write *dobj,
char *(*pfn) (const char *),
re PR go/78628 (GO fails to build a translation unit decl) 2017-08-21 Richard Biener <rguenther@suse.de> include/ * simple-object.h (simple_object_copy_lto_debug_sections): New function. libiberty/ * simple-object-common.h (struct simple_object_functions): Add copy_lto_debug_sections hook. * simple-object.c: Include fcntl.h. (handle_lto_debug_sections): New helper function. (simple_object_copy_lto_debug_sections): New function copying early LTO debug sections to regular debug sections in a new file. (simple_object_start_write): Handle NULL segment_name. * simple-object-coff.c (simple_object_coff_functions): Adjust for not implemented copy_lto_debug_sections hook. * simple-object-mach-o.c (simple_object_mach_o_functions): Likewise. * simple-object-xcoff.c (simple_object_xcoff_functions): Likewise. * simple-object-elf.c (SHT_NULL, SHT_SYMTAB, SHT_RELA, SHT_REL, SHT_GROUP): Add various sectopn header types. (SHF_EXCLUDE): Add flag. (Elf32_External_Sym, Elf64_External_Sym): Add symbol struct. (ELF_ST_BIND, ELF_ST_TYPE, ELF_ST_INFO): Add accessors. (STT_OBJECT, STT_FUNC, STT_TLS, STT_GNU_IFUNC): Add Symbol types. (STV_DEFAULT): Add symbol visibility. (SHN_COMMON): Add special section index name. (struct simple_object_elf_write): New. (simple_object_elf_start_write): Adjust for new private data. (simple_object_elf_write_shdr): Pass in values for all fields we write. (simple_object_elf_write_to_file): Adjust. Copy from recorded section headers if requested. (simple_object_elf_release_write): Release private data. (simple_object_elf_copy_lto_debug_sections): Copy and rename sections as denoted by PFN and all their dependences, symbols and relocations to the empty destination file. (simple_object_elf_functions): Adjust for copy_lto_debug_sections hook. gcc/ * debug.h (struct gcc_debug_hooks): Add die_ref_for_decl and register_external_die hooks. (debug_false_tree_charstarstar_uhwistar): Declare. (debug_nothing_tree_charstar_uhwi): Likewise. * debug.c (do_nothing_debug_hooks): Adjust. (debug_false_tree_charstarstar_uhwistar): New do nothing. (debug_nothing_tree_charstar_uhwi): Likewise. * dbxout.c (dbx_debug_hooks): Adjust. (xcoff_debug_hooks): Likewise. * sdbout.c (sdb_debug_hooks): Likewise. * vmsdbgout.c (vmsdbg_debug_hooks): Likewise. * dwarf2out.c (macinfo_label_base): New global. (dwarf2out_register_external_die): New function for the register_external_die hook. (dwarf2out_die_ref_for_decl): Likewise for die_ref_for_decl. (dwarf2_debug_hooks): Use them. (dwarf2_lineno_debug_hooks): Adjust. (struct die_struct): Add with_offset flag. (DEBUG_LTO_DWO_INFO_SECTION, DEBUG_LTO_INFO_SECTION, DEBUG_LTO_DWO_ABBREV_SECTION, DEBUG_LTO_ABBREV_SECTION, DEBUG_LTO_DWO_MACINFO_SECTION, DEBUG_LTO_MACINFO_SECTION, DEBUG_LTO_DWO_MACRO_SECTION, DEBUG_LTO_MACRO_SECTION, DEBUG_LTO_LINE_SECTION, DEBUG_LTO_DWO_STR_OFFSETS_SECTION, DEBUG_LTO_STR_DWO_SECTION, DEBUG_STR_LTO_SECTION): New macros defining section names for the early LTO debug variants. (reset_indirect_string): New helper. (add_AT_external_die_ref): Helper for dwarf2out_register_external_die. (print_dw_val): Add support for offsetted symbol references. (get_ultimate_context): Split out from is_cxx. (is_cxx): Use get_ultimate_context. (is_fortran): Add decl overload. (compute_comp_unit_symbol): Split out worker from compute_section_prefix. (compute_section_prefix): Call compute_comp_unit_symbol and set comdat_type_p here. (output_die): Skip DIE symbol output for the LTO added one. Handle DIE symbol references with offset. (output_comp_unit): Guard section name mangling properly. For LTO debug sections emit a symbol at the section beginning which we use to refer to its DIEs. (add_abstract_origin_attribute): For DIEs registered via dwarf2out_register_external_die directly refer to the early DIE rather than indirectly through the shadow one we created. Remove obsolete call to dwarf2out_abstract_function for non-function/block origins. (gen_array_type_die): When generating early LTO debug do not emit DW_AT_string_length. (gen_formal_parameter_die): Do not re-create DIEs for PARM_DECLs late when in LTO. As suggested place a gcc_unreachable for the DECL_ABSTRACT_P case. (gen_subprogram_die): Avoid another specification DIE for early built declarations/definitions for the late LTO case. (gen_variable_die): Add type references for late duplicated VLA dies when in late LTO. (gen_inlined_subroutine_die): Do not call dwarf2out_abstract_function, we have the abstract instance already. (process_scope_var): Adjust decl DIE contexts in LTO which first puts them in limbo. (gen_decl_die): Do not generate type DIEs late apart from types for VLAs or for decls we do not yet have a DIE. Do not call dwarf2out_abstract_function late. (dwarf2out_early_global_decl): Make sure to create DIEs for abstract instances of a decl first. (dwarf2out_late_global_decl): Adjust comment. (output_macinfo_op): With multiple macro sections use macinfo_label_base to distinguish labels. (output_macinfo): Likewise. Update macinfo_label_base. Pass in the line info label. (note_variable_value_in_expr): When generating LTO resolve all variable values here by generating DIEs as needed. (init_sections_and_labels): Add early LTO debug flag parameter and generate different sections and names if set. Add generation counter for the labels so we can have multiple of them. (reset_dies): Helper to allow DIEs to be output multiple times. (dwarf2out_finish): When outputting DIEs to the fat part of an LTO object first reset DIEs. (dwarf2out_early_finish): Output early DIEs when generating LTO. (modified_type_die): Check for decl_ultimate_origin being self before recursing. (gen_type_die_with_usage): Likewise. (gen_typedef_die): Allow decl_ultimate_origin being self. (set_decl_abstract_flags): Remove. (set_block_abstract_flags): Likewise. (dwarf2out_abstract_function): Treat the early generated DIEs as the abstract copy and only add DW_AT_inline and DW_AT_artificial here and call set_decl_origin_self. If the DIE has an abstract origin don't do anything. * tree.c (free_lang_data): Build a dummy TRANSLATION_UNIT_DECL if we have none yet (Go fails to build one, PR78628). (variably_modified_type_p): Prevent endless recursion for Ada cyclic pointer types. * lto-streamer-in.c: Include debug.h. (dref_queue): New global. (lto_read_tree_1): Stream in DIE references. (lto_input_tree): Register DIE references. (input_function): Stream DECL_DEBUG_ARGS. * lto-streamer-out.c: Include debug.h. (lto_write_tree_1): Output DIE references. (DFS::DFS_write_tree_body): Follow DECL_ABSTRACT_ORIGIN. Force a TRANSLATION_UNIT_DECL DECL_CONTEXT for file-scope decls. (output_function): Stream DECL_DEBUG_ARGS. * tree-streamer-in.c (lto_input_ts_decl_common_tree_pointers): Stream DECL_ABSTRACT_ORIGIN. * tree-streamer-out.c (write_ts_decl_common_tree_pointers): Likewise. (write_ts_decl_minimal_tree_pointers): Force a TRANSLATION_UNIT_DECL DECL_CONTEXT for file-scope decls. * lto-streamer.h (struct dref_entry): Declare. (dref_queue): Likewise. * cfgexpand.c (pass_expand::execute): Do not call the outlining_inline_function hook here. * lto-wrapper.c (debug_obj): New global. (tool_cleanup): Unlink it if required. (debug_objcopy): New function. (run_gcc): Handle early debug sections in the IL files by extracting them to separate files, partially linkin them and feeding the result back as result to the linker. * config/darwin.h (DEBUG_LTO_INFO_SECTION, DEBUG_LTO_ABBREV_SECTION, DEBUG_LTO_MACINFO_SECTION, DEBUG_LTO_LINE_SECTION, DEBUG_STR_LTO_SECTION, DEBUG_LTO_MACRO_SECTION): Put early debug sections into a separate segment. * config/darwin.c (darwin_asm_named_section): Handle __GNU_DWARF_LTO segments. (darwin_asm_dwarf_section): Likewise. (darwin_asm_output_dwarf_offset): Likewise. * config/i386/i386.c (make_resolver_func): Set DECL_IGNORED_P. lto/ * lto.c (unify_scc): Truncate DIE reference queue for dropped SCCs. (lto_read_decls): Process TRANSLATION_UNIT_DECLs. Remove TYPE_DECL debug processing, register DIE references from prevailing SCCs with the debug machinery. (lto_section_with_id): Handle LTO debug sections. libstdc++/ * testsuite/libstdc++-prettyprinters/prettyprinters.exp: Run all tests with -flto as well if supported. testsuite/ * c-c++-common/asan/global-overflow-1.c: Adjust diagnostic location regex to handle the LTO case. * c-c++-common/asan/heap-overflow-1.c: Likewise. * c-c++-common/asan/misalign-1.c: Likewise. * c-c++-common/asan/misalign-2.c: Likewise. * c-c++-common/asan/null-deref-1.c: Likewise. * c-c++-common/asan/stack-overflow-1.c: Likewise. * c-c++-common/asan/strncpy-overflow-1.c: Likewise. * c-c++-common/asan/use-after-free-1.c: Likewise. * c-c++-common/asan/alloca_big_alignment.c: Likewise. * c-c++-common/asan/alloca_detect_custom_size.c: Likewise. * c-c++-common/asan/alloca_overflow_partial.c: Likewise. * c-c++-common/asan/alloca_overflow_right.c: Likewise. * c-c++-common/asan/alloca_underflow_left.c: Likewise. * g++.dg/asan/large-func-test-1.C: Likewise. * gfortran.dg/save_6.f90: Add -flto -g variant of save_5.f90. From-SVN: r251220
2017-08-21 12:29:00 +02:00
int *err);
};
/* The known object file formats. */
extern const struct simple_object_functions simple_object_coff_functions;
extern const struct simple_object_functions simple_object_elf_functions;
extern const struct simple_object_functions simple_object_mach_o_functions;
extern const struct simple_object_functions simple_object_xcoff_functions;
/* Read SIZE bytes from DESCRIPTOR at file offset OFFSET into BUFFER.
Return non-zero on success. On failure return 0 and set *ERRMSG
and *ERR. */
extern int
simple_object_internal_read (int descriptor, off_t offset,
unsigned char *buffer, size_t size,
const char **errmsg, int *err);
/* Write SIZE bytes from BUFFER to DESCRIPTOR at file offset OFFSET.
Return non-zero on success. On failure return 0 and set *ERRMSG
and *ERR. */
extern int
simple_object_internal_write (int descriptor, off_t offset,
const unsigned char *buffer, size_t size,
const char **errmsg, int *err);
/* Define ulong_type as an unsigned 64-bit type if available.
Otherwise just make it unsigned long. */
#ifdef UNSIGNED_64BIT_TYPE
__extension__ typedef UNSIGNED_64BIT_TYPE ulong_type;
#else
typedef unsigned long ulong_type;
#endif
/* Fetch a big-endian 16-bit value. */
static inline unsigned short
simple_object_fetch_big_16 (const unsigned char *buf)
{
return ((unsigned short) buf[0] << 8) | (unsigned short) buf[1];
}
/* Fetch a little-endian 16-bit value. */
static inline unsigned short
simple_object_fetch_little_16 (const unsigned char *buf)
{
return ((unsigned short) buf[1] << 8) | (unsigned short) buf[0];
}
/* Fetch a big-endian 32-bit value. */
static inline unsigned int
simple_object_fetch_big_32 (const unsigned char *buf)
{
return (((unsigned int) buf[0] << 24)
| ((unsigned int) buf[1] << 16)
| ((unsigned int) buf[2] << 8)
| (unsigned int) buf[3]);
}
/* Fetch a little-endian 32-bit value. */
static inline unsigned int
simple_object_fetch_little_32 (const unsigned char *buf)
{
return (((unsigned int) buf[3] << 24)
| ((unsigned int) buf[2] << 16)
| ((unsigned int) buf[1] << 8)
| (unsigned int) buf[0]);
}
/* Fetch a big-endian 32-bit value as a ulong_type. */
static inline ulong_type
simple_object_fetch_big_32_ulong (const unsigned char *buf)
{
return (ulong_type) simple_object_fetch_big_32 (buf);
}
/* Fetch a little-endian 32-bit value as a ulong_type. */
static inline ulong_type
simple_object_fetch_little_32_ulong (const unsigned char *buf)
{
return (ulong_type) simple_object_fetch_little_32 (buf);
}
#ifdef UNSIGNED_64BIT_TYPE
/* Fetch a big-endian 64-bit value. */
static inline ulong_type
simple_object_fetch_big_64 (const unsigned char *buf)
{
return (((ulong_type) buf[0] << 56)
| ((ulong_type) buf[1] << 48)
| ((ulong_type) buf[2] << 40)
| ((ulong_type) buf[3] << 32)
| ((ulong_type) buf[4] << 24)
| ((ulong_type) buf[5] << 16)
| ((ulong_type) buf[6] << 8)
| (ulong_type) buf[7]);
}
/* Fetch a little-endian 64-bit value. */
static inline ulong_type
simple_object_fetch_little_64 (const unsigned char *buf)
{
return (((ulong_type) buf[7] << 56)
| ((ulong_type) buf[6] << 48)
| ((ulong_type) buf[5] << 40)
| ((ulong_type) buf[4] << 32)
| ((ulong_type) buf[3] << 24)
| ((ulong_type) buf[2] << 16)
| ((ulong_type) buf[1] << 8)
| (ulong_type) buf[0]);
}
#endif
/* Store a big-endian 16-bit value. */
static inline void
simple_object_set_big_16 (unsigned char *buf, unsigned short val)
{
buf[0] = (val >> 8) & 0xff;
buf[1] = val & 0xff;
}
/* Store a little-endian 16-bit value. */
static inline void
simple_object_set_little_16 (unsigned char *buf, unsigned short val)
{
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
}
/* Store a big-endian 32-bit value. */
static inline void
simple_object_set_big_32 (unsigned char *buf, unsigned int val)
{
buf[0] = (val >> 24) & 0xff;
buf[1] = (val >> 16) & 0xff;
buf[2] = (val >> 8) & 0xff;
buf[3] = val & 0xff;
}
/* Store a little-endian 32-bit value. */
static inline void
simple_object_set_little_32 (unsigned char *buf, unsigned int val)
{
buf[3] = (val >> 24) & 0xff;
buf[2] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
}
/* Store a big-endian 32-bit value coming in as a ulong_type. */
static inline void
simple_object_set_big_32_ulong (unsigned char *buf, ulong_type val)
{
simple_object_set_big_32 (buf, val);
}
/* Store a little-endian 32-bit value coming in as a ulong_type. */
static inline void
simple_object_set_little_32_ulong (unsigned char *buf, ulong_type val)
{
simple_object_set_little_32 (buf, val);
}
#ifdef UNSIGNED_64BIT_TYPE
/* Store a big-endian 64-bit value. */
static inline void
simple_object_set_big_64 (unsigned char *buf, ulong_type val)
{
buf[0] = (val >> 56) & 0xff;
buf[1] = (val >> 48) & 0xff;
buf[2] = (val >> 40) & 0xff;
buf[3] = (val >> 32) & 0xff;
buf[4] = (val >> 24) & 0xff;
buf[5] = (val >> 16) & 0xff;
buf[6] = (val >> 8) & 0xff;
buf[7] = val & 0xff;
}
/* Store a little-endian 64-bit value. */
static inline void
simple_object_set_little_64 (unsigned char *buf, ulong_type val)
{
buf[7] = (val >> 56) & 0xff;
buf[6] = (val >> 48) & 0xff;
buf[5] = (val >> 40) & 0xff;
buf[4] = (val >> 32) & 0xff;
buf[3] = (val >> 24) & 0xff;
buf[2] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
}
#endif