1 /*
2 Copyright (c) 2018, David Anderson
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8 
9     Redistributions of source code must retain the above
10     copyright notice, this list of conditions and the following
11     disclaimer.
12 
13     Redistributions in binary form must reproduce the above
14     copyright notice, this list of conditions and the following
15     disclaimer in the documentation and/or other materials
16     provided with the distribution.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 */
33 #ifndef DWARF_MACHOREAD_H
34 #define DWARF_MACHOREAD_H
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 
40 struct generic_macho_header {
41     Dwarf_Unsigned   magic;
42     Dwarf_Unsigned   cputype;
43     Dwarf_Unsigned   cpusubtype;
44     Dwarf_Unsigned   filetype;
45     Dwarf_Unsigned   ncmds;      /* number of load commands */
46     Dwarf_Unsigned   sizeofcmds; /* the size of all the load commands */
47     Dwarf_Unsigned   flags;
48     Dwarf_Unsigned   reserved;
49 };
50 struct generic_macho_command {
51     Dwarf_Unsigned   cmd;
52     Dwarf_Unsigned   cmdsize;
53     Dwarf_Unsigned   offset_this_command;
54 };
55 
56 struct generic_macho_segment_command {
57     Dwarf_Unsigned   cmd;
58     Dwarf_Unsigned   cmdsize;
59     char             segname[24];
60     Dwarf_Unsigned   vmaddr;
61     Dwarf_Unsigned   vmsize;
62     Dwarf_Unsigned   fileoff;
63     Dwarf_Unsigned   filesize;
64     Dwarf_Unsigned   maxprot;
65     Dwarf_Unsigned   initprot;
66     Dwarf_Unsigned   nsects;
67     Dwarf_Unsigned   flags;
68 
69     /* our index into mo_commands */
70     Dwarf_Unsigned   macho_command_index;
71     Dwarf_Unsigned   sectionsoffset;
72 };
73 
74 struct generic_macho_section {
75     /* Larger than in file, room for NUL guaranteed */
76     char          sectname[24];
77     char          segname[24];
78     const char *  dwarfsectname;
79     Dwarf_Unsigned  addr;
80     Dwarf_Unsigned  size;
81     Dwarf_Unsigned  offset;
82     Dwarf_Unsigned  align;
83     Dwarf_Unsigned  reloff;
84     Dwarf_Unsigned  nreloc;
85     Dwarf_Unsigned  flags;
86     Dwarf_Unsigned  reserved1;
87     Dwarf_Unsigned  reserved2;
88     Dwarf_Unsigned  reserved3;
89     Dwarf_Unsigned  generic_segment_num;
90     Dwarf_Unsigned  offset_of_sec_rec;
91     Dwarf_Small*  loaded_data;
92 };
93 
94 
95 /*  ident[0] == 'M' means this is a macho header.
96     ident[1] will be 1 indicating version 1.
97     Other bytes in ident not defined, should be zero. */
98 typedef struct dwarf_macho_filedata_s {
99     char             mo_ident[8];
100     const char *     mo_path; /* libdwarf must free.*/
101     int              mo_fd;
102     int              mo_destruct_close_fd; /*aka: lib owns fd */
103     int              mo_is_64bit;
104     Dwarf_Unsigned   mo_filesize;
105     Dwarf_Small      mo_offsetsize; /* 32 or 64 section data */
106     Dwarf_Small      mo_pointersize;
107     int              mo_ftype;
108     Dwarf_Endianness mo_endian;
109     /*Dwarf_Small      mo_machine; */
110     void (*mo_copy_word) (void *, const void *, unsigned long);
111 
112     /* Used to hold 32 and 64 header data */
113     struct generic_macho_header mo_header;
114 
115     unsigned mo_command_count;
116     Dwarf_Unsigned  mo_command_start_offset;
117     struct generic_macho_command *mo_commands;
118     Dwarf_Unsigned  mo_offset_after_commands;
119 
120     Dwarf_Unsigned mo_segment_count;
121     struct generic_macho_segment_command *mo_segment_commands;
122 
123     Dwarf_Unsigned mo_dwarf_sectioncount;
124     struct generic_macho_section *mo_dwarf_sections;
125 } dwarf_macho_object_access_internals_t;
126 
127 int dwarf_load_macho_header(dwarf_macho_object_access_internals_t * mfp,
128     int *errcode);
129 int dwarf_load_macho_commands(dwarf_macho_object_access_internals_t * mfp,
130     int *errcode);
131 
132 #ifdef __cplusplus
133 }
134 #endif /* __cplusplus */
135 #endif /* DWARF_MACHOREAD_H */
136