summaryrefslogtreecommitdiff
path: root/libdwarf/pro_init.c
blob: d2931369972c8018375f8e0125ed4620a115164c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*

  Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved.
  Portions Copyright 2008-2011 David Anderson, Inc. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement 
  or the like.  Any license provided herein, whether implied or 
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with 
  other software, or any other product whatsoever.  

  You should have received a copy of the GNU Lesser General Public 
  License along with this program; if not, write the Free Software 
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
  USA.

  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  Mountain View, CA 94043, or:

  http://www.sgi.com

  For further information regarding this notice, see:

  http://oss.sgi.com/projects/GenInfo/NoticeExplan

*/



#include "config.h"
#include "libdwarfdefs.h"
#include <stdio.h>
#include <string.h>
#include "pro_incl.h"
#include "pro_section.h"        /* for MAGIC_SECT_NO */
#include "pro_reloc_symbolic.h"
#include "pro_reloc_stream.h"


static void common_init(Dwarf_P_Debug dbg, Dwarf_Unsigned flags);

void *_dwarf_memcpy_swap_bytes(void *s1, const void *s2, size_t len);

/*  This function sets up a new dwarf producing region. 
    flags: Indicates type of access method, one of DW_DLC* macros
    func(): Used to create a new object file, a call back function
    errhand(): Error Handler provided by user
    errarg: Argument to errhand()
    error: returned error value */
    /*  We want the following to have an elf section number that matches 
        'nothing' */
static struct Dwarf_P_Section_Data_s init_sect = {
    MAGIC_SECT_NO, 0, 0, 0, 0
};

/* New June, 2011, this is the latest, most flexible
   version. It adds (compared to *_b) the user_data
   pointer which is passed back (unchanged) in 
   each callback call. */
Dwarf_P_Debug
dwarf_producer_init_c(Dwarf_Unsigned flags,
    Dwarf_Callback_Func_c func,
    Dwarf_Handler errhand,
    Dwarf_Ptr errarg, 
    void * user_data,
    Dwarf_Error * error)
{
    Dwarf_P_Debug dbg;
    dbg = (Dwarf_P_Debug) _dwarf_p_get_alloc(NULL,
        sizeof(struct Dwarf_P_Debug_s));
    if (dbg == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC,
            (Dwarf_P_Debug) DW_DLV_BADADDR);
    }
    memset((void *) dbg, 0, sizeof(struct Dwarf_P_Debug_s));
    /* For the time being */
    if (func == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_NO_CALLBACK_FUNC,
            (Dwarf_P_Debug) DW_DLV_BADADDR);
    }
    dbg->de_callback_func_c = func;
    dbg->de_errhand = errhand;
    dbg->de_errarg = errarg;
    dbg->de_user_data = user_data;
    common_init(dbg, flags);
    return dbg;

}


Dwarf_P_Debug
dwarf_producer_init_b(Dwarf_Unsigned flags,
    Dwarf_Callback_Func_b func,
    Dwarf_Handler errhand,
    Dwarf_Ptr errarg, Dwarf_Error * error)
{
    Dwarf_P_Debug dbg;
    dbg = (Dwarf_P_Debug) _dwarf_p_get_alloc(NULL,
        sizeof(struct Dwarf_P_Debug_s));
    if (dbg == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC,
            (Dwarf_P_Debug) DW_DLV_BADADDR);
    }
    memset((void *) dbg, 0, sizeof(struct Dwarf_P_Debug_s));
    /* For the time being */
    if (func == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_NO_CALLBACK_FUNC,
            (Dwarf_P_Debug) DW_DLV_BADADDR);
    }
    dbg->de_callback_func_b = func;
    dbg->de_errhand = errhand;
    dbg->de_errarg = errarg;
    dbg->de_user_data = 0;
    common_init(dbg, flags);
    return dbg;

}

Dwarf_P_Debug
dwarf_producer_init(Dwarf_Unsigned flags,
    Dwarf_Callback_Func func,
    Dwarf_Handler errhand,
    Dwarf_Ptr errarg, Dwarf_Error * error)
{
    Dwarf_P_Debug dbg = 0;

    dbg = (Dwarf_P_Debug) _dwarf_p_get_alloc(NULL,
        sizeof(struct Dwarf_P_Debug_s));
    if (dbg == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC,
            (Dwarf_P_Debug) DW_DLV_BADADDR);
    }
    memset((void *) dbg, 0, sizeof(struct Dwarf_P_Debug_s));
    /* For the time being */
    if (func == NULL) {
        DWARF_P_DBG_ERROR(dbg, DW_DLE_NO_CALLBACK_FUNC,
            (Dwarf_P_Debug) DW_DLV_BADADDR);
    }
    dbg->de_callback_func = func;
    dbg->de_errhand = errhand;
    dbg->de_errarg = errarg;
    dbg->de_user_data = 0;
    common_init(dbg, flags);
    return dbg;
}
static void
common_init(Dwarf_P_Debug dbg, Dwarf_Unsigned flags)
{
    unsigned int k = 0;

    dbg->de_version_magic_number = PRO_VERSION_MAGIC;
    dbg->de_n_debug_sect = 0;
    dbg->de_debug_sects = &init_sect;
    dbg->de_current_active_section = &init_sect;
    dbg->de_flags = flags;
    _dwarf_init_default_line_header_vals(dbg);

    /* Now, with flags set, can use 64bit tests */



#if defined(HAVE_STRICT_DWARF2_32BIT_OFFSET)
    /*  This is cygnus 32bit offset, as specified in pure dwarf2 v2.0.0.
        It is consistent with normal DWARF2/3 generation of always
        generating 32 bit offsets. */
    dbg->de_64bit_extension = 0;
    dbg->de_pointer_size = (IS_64BIT(dbg) ? 8 : 4);
    dbg->de_offset_size = (IS_64BIT(dbg) ? 4 : 4);
    dbg->de_ptr_reloc =
        IS_64BIT(dbg) ? Get_REL64_isa(dbg) : Get_REL32_isa(dbg);
    /*  non-MIPS, dwarf lengths and offsets are 32 bits even for 64bit
        pointer environments. */
    /*  Get_REL32_isa here supports 64-bit-pointer dwarf with pure
        dwarf2 v2.0.0 32bit offsets, as emitted by cygnus tools. And
        pure 32 bit offset dwarf for 32bit pointer apps. */

    dbg->de_offset_reloc = Get_REL32_isa(dbg);
#elif defined(HAVE_SGI_IRIX_OFFSETS) 
    /*  MIPS-SGI-IRIX 32 or 64, where offsets and lengths are both 64 bit for 
        64bit pointer objects and both 32 bit for 32bit pointer objects. 
        And a dwarf-reader must check elf info to tell which applies. */
    dbg->de_64bit_extension = 0;
    dbg->de_pointer_size = (IS_64BIT(dbg) ? 8 : 4);
    dbg->de_offset_size = (IS_64BIT(dbg) ? 8 : 4);
    dbg->de_ptr_reloc =
        IS_64BIT(dbg) ? Get_REL64_isa(dbg) : Get_REL32_isa(dbg);
    dbg->de_offset_reloc = dbg->de_ptr_reloc;
#else /* HAVE_DWARF2_99_EXTENSION or default. */ 
    /*  Revised 64 bit output, using distingushed values. Per 1999
        dwarf3.  This allows run-time selection of offset size.  */
    dbg->de_64bit_extension = (IS_64BIT(dbg) ? 1 : 0);
    dbg->de_pointer_size = (IS_64BIT(dbg) ? 8 : 4);
    if( flags & DW_DLC_OFFSET_SIZE_64 && (dbg->de_pointer_size == 8)) {
        /*  When it's 64 bit address, a 64bit offset is sensible.
            Arguably a 32 bit address with 64 bit offset could be
            sensible, but who would want that? */
        dbg->de_offset_size = 8;
        dbg->de_64bit_extension = 1;
    }  else {
        dbg->de_offset_size = 4;
        dbg->de_64bit_extension = 0;
    }
    dbg->de_ptr_reloc =
        IS_64BIT(dbg) ? Get_REL64_isa(dbg) : Get_REL32_isa(dbg);
    /*  Non-MIPS, dwarf lengths and offsets are 32 bits even for 64bit
        pointer environments. */
    /*  Get_REL??_isa here supports 64bit-offset dwarf. For 64bit, we
        emit the extension bytes. */

    dbg->de_offset_reloc = IS_64BIT(dbg) ? Get_REL64_isa(dbg)
        : Get_REL32_isa(dbg);
#endif /*  HAVE_DWARF2_99_EXTENSION etc. */

    dbg->de_exc_reloc = Get_REL_SEGREL_isa(dbg);

    dbg->de_is_64bit = IS_64BIT(dbg);


    if (flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
        dbg->de_relocation_record_size =
            sizeof(struct Dwarf_Relocation_Data_s);
    } else {
        
#if HAVE_ELF64_GETEHDR
        dbg->de_relocation_record_size =
            IS_64BIT(dbg)? sizeof(REL64) : sizeof(REL32);
#else
        dbg->de_relocation_record_size = sizeof(REL32);
#endif

    }

    if (dbg->de_offset_size == 8) {
        dbg->de_ar_data_attribute_form = DW_FORM_data8;
        dbg->de_ar_ref_attr_form = DW_FORM_ref8;
    } else {
        dbg->de_ar_data_attribute_form = DW_FORM_data4;
        dbg->de_ar_ref_attr_form = DW_FORM_ref4;
    }

    if (flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
        dbg->de_reloc_name = _dwarf_pro_reloc_name_symbolic;
        dbg->de_reloc_pair = _dwarf_pro_reloc_length_symbolic;
        dbg->de_transform_relocs_to_disk =
            _dwarf_symbolic_relocs_to_disk;
    } else {
        if (IS_64BIT(dbg)) {
            dbg->de_reloc_name = _dwarf_pro_reloc_name_stream64;
        } else {
            dbg->de_reloc_name = _dwarf_pro_reloc_name_stream32;
        }
        dbg->de_reloc_pair = 0;
        dbg->de_transform_relocs_to_disk = _dwarf_stream_relocs_to_disk;
    }
    for (k = 0; k < NUM_DEBUG_SECTIONS; ++k) {

        Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[k];

        prel->pr_slots_per_block_to_alloc = DEFAULT_SLOTS_PER_BLOCK;
    }
    /* First assume host, target same endianness */
    dbg->de_same_endian = 1;
    dbg->de_copy_word = memcpy;
#ifdef WORDS_BIGENDIAN
    /* host is big endian, so what endian is target? */
    if (flags & DW_DLC_TARGET_LITTLEENDIAN) {
        dbg->de_same_endian = 0;
        dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
    }
#else /* little endian */
    /* host is little endian, so what endian is target? */
    if (flags & DW_DLC_TARGET_BIGENDIAN) {
        dbg->de_same_endian = 0;
        dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
    }
#endif /* !WORDS_BIGENDIAN */


    return;

}