1/*
2
3  Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
4
5  This program is free software; you can redistribute it and/or modify it
6  under the terms of version 2.1 of the GNU Lesser General Public License
7  as published by the Free Software Foundation.
8
9  This program is distributed in the hope that it would be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13  Further, this software is distributed without any warranty that it is
14  free of the rightful claim of any third person regarding infringement
15  or the like.  Any license provided herein, whether implied or
16  otherwise, applies only to this software file.  Patent licenses, if
17  any, provided herein do not apply to combinations of this program with
18  other software, or any other product whatsoever.
19
20  You should have received a copy of the GNU Lesser General Public
21  License along with this program; if not, write the Free Software
22  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
23  USA.
24
25  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
26  Mountain View, CA 94043, or:
27
28  http://www.sgi.com
29
30  For further information regarding this notice, see:
31
32  http://oss.sgi.com/projects/GenInfo/NoticeExplan
33
34*/
35
36
37
38#include "config.h"
39#include "libdwarfdefs.h"
40#include <stdio.h>
41#include <string.h>
42#ifdef HAVE_ELFACCESS_H
43#include <elfaccess.h>
44#endif
45#include "pro_incl.h"
46#include "pro_section.h"
47
48
49/*
50    This function adds another type name to the
51    list of type names for the given Dwarf_P_Debug.
52    It returns 0 on error, and 1 otherwise.
53*/
54Dwarf_Unsigned
55dwarf_add_typename(Dwarf_P_Debug dbg,
56                   Dwarf_P_Die die,
57                   char *type_name, Dwarf_Error * error)
58{
59    return
60        _dwarf_add_simple_name_entry(dbg, die, type_name,
61                                     dwarf_snk_typename, error);
62}
63
64/*
65  The following is the generic 'add a simple name entry'
66  for any of the simple name sections.
67
68  See enum dwarf_sn_kind in pro_opaque.h
69
70*/
71Dwarf_Unsigned
72_dwarf_add_simple_name_entry(Dwarf_P_Debug dbg,
73                             Dwarf_P_Die die,
74                             char *entry_name,
75                             enum dwarf_sn_kind entrykind,
76                             Dwarf_Error * error)
77{
78    Dwarf_P_Simple_nameentry nameentry;
79    Dwarf_P_Simple_name_header hdr;
80    char *name;
81    int uword_size;
82
83    if (dbg == NULL) {
84        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
85        return (0);
86    }
87
88    if (die == NULL) {
89        _dwarf_p_error(NULL, error, DW_DLE_DIE_NULL);
90        return (0);
91    }
92
93
94    nameentry = (Dwarf_P_Simple_nameentry)
95        _dwarf_p_get_alloc(dbg,
96                           sizeof(struct Dwarf_P_Simple_nameentry_s));
97    if (nameentry == NULL) {
98        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
99        return (0);
100    }
101
102    name = _dwarf_p_get_alloc(dbg, strlen(entry_name) + 1);
103    if (name == NULL) {
104        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
105        return (0);
106    }
107    strcpy(name, entry_name);
108
109    nameentry->sne_die = die;
110    nameentry->sne_name = name;
111    nameentry->sne_name_len = strlen(name);
112    uword_size = dbg->de_offset_size;
113
114    hdr = &dbg->de_simple_name_headers[entrykind];
115    if (hdr->sn_head == NULL)
116        hdr->sn_head = hdr->sn_tail = nameentry;
117    else {
118        hdr->sn_tail->sne_next = nameentry;
119        hdr->sn_tail = nameentry;
120    }
121    hdr->sn_count++;
122    hdr->sn_net_len += uword_size + nameentry->sne_name_len + 1;
123
124    return (1);
125}
126
127
128
129/*
130     _dwarf_transform_simplename_to_disk writes
131     ".rel.debug_pubnames",
132     ".rel.debug_funcnames",       sgi extension
133     ".rel.debug_typenames",       sgi extension
134     ".rel.debug_varnames",        sgi extension
135     ".rel.debug_weaknames",       sgi extension
136     to disk.
137        section_index indexes one of those sections.
138        entrykind is one of those 'kind's.
139
140*/
141int
142_dwarf_transform_simplename_to_disk(Dwarf_P_Debug dbg, enum dwarf_sn_kind entrykind, int section_index, /* in
143                                                                                                           de_elf_sects
144                                                                                                           etc
145                                                                                                         */
146                                    Dwarf_Error * error)
147{
148
149
150    /* Used to fill in 0. */
151    const Dwarf_Signed big_zero = 0;
152
153    /* Used to scan the section data buffers. */
154    Dwarf_P_Section_Data debug_sect;
155
156    Dwarf_Signed debug_info_size;
157
158    Dwarf_P_Simple_nameentry nameentry_original;
159    Dwarf_P_Simple_nameentry nameentry;
160    Dwarf_Small *stream_bytes;
161    Dwarf_Small *cur_stream_bytes_ptr;
162    Dwarf_Unsigned stream_bytes_count;
163    Dwarf_Unsigned adjusted_length;     /* count excluding length field
164                                         */
165
166
167    int uword_size = dbg->de_offset_size;
168    int extension_size = dbg->de_64bit_extension ? 4 : 0;
169
170    Dwarf_P_Simple_name_header hdr;
171
172
173    /* ***** BEGIN CODE ***** */
174
175    debug_info_size = 0;
176    for (debug_sect = dbg->de_debug_sects; debug_sect != NULL;
177         debug_sect = debug_sect->ds_next) {
178        /* We want the size of the .debug_info section for this CU
179           because the dwarf spec requires us to output it below so we
180           look for it specifically. */
181        if (debug_sect->ds_elf_sect_no == dbg->de_elf_sects[DEBUG_INFO]) {
182            debug_info_size += debug_sect->ds_nbytes;
183        }
184    }
185
186    hdr = &dbg->de_simple_name_headers[entrykind];
187    /* Size of the .debug_typenames (or similar) section header. */
188    stream_bytes_count = extension_size + uword_size +  /* Size of
189                                                           length
190                                                           field. */
191        sizeof(Dwarf_Half) +    /* Size of version field. */
192        uword_size +            /* Size of .debug_info offset. */
193        uword_size;             /* Size of .debug_names. */
194
195
196
197    nameentry_original = hdr->sn_head;
198    nameentry = nameentry_original;
199    /* add in the content size */
200    stream_bytes_count += hdr->sn_net_len;
201
202    /* Size of the last 0 offset. */
203    stream_bytes_count += uword_size;
204
205    /* Now we know how long the entire section is */
206    GET_CHUNK(dbg, dbg->de_elf_sects[section_index],
207              stream_bytes, (unsigned long) stream_bytes_count, error);
208    if (stream_bytes == NULL) {
209        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
210        return (0);
211    }
212    cur_stream_bytes_ptr = stream_bytes;
213
214    if (extension_size) {
215        Dwarf_Unsigned x = DISTINGUISHED_VALUE;
216
217        WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
218                        (const void *) &x, sizeof(x), extension_size);
219        cur_stream_bytes_ptr += extension_size;
220
221    }
222    /* Write the adjusted length of .debug_*names section. */
223    adjusted_length = stream_bytes_count - uword_size - extension_size;
224    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
225                    (const void *) &adjusted_length,
226                    sizeof(adjusted_length), uword_size);
227    cur_stream_bytes_ptr += uword_size;
228
229    /* Write the version as 2 bytes. */
230    {
231        Dwarf_Half verstamp = CURRENT_VERSION_STAMP;
232
233        WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
234                        (const void *) &verstamp,
235                        sizeof(verstamp), sizeof(Dwarf_Half));
236        cur_stream_bytes_ptr += sizeof(Dwarf_Half);
237    }
238
239    /* Write the offset of the compile-unit. */
240    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
241                    (const void *) &big_zero,
242                    sizeof(big_zero), uword_size);
243    cur_stream_bytes_ptr += uword_size;
244
245    /* now create the relocation for the compile_unit offset */
246    {
247        int res = dbg->de_reloc_name(dbg,
248                                     section_index,
249                                     extension_size + uword_size +
250                                     sizeof(Dwarf_Half)
251                                     /* r_offset */
252                                     ,
253                                     /* debug_info section name symbol */
254                                     dbg->de_sect_name_idx[DEBUG_INFO],
255                                     dwarf_drt_data_reloc,
256                                     uword_size);
257
258        if (res != DW_DLV_OK) {
259            {
260                _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
261                return (0);
262            }
263        }
264    }
265
266    /* Write the size of .debug_info section. */
267    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
268                    (const void *) &debug_info_size,
269                    sizeof(debug_info_size), uword_size);
270    cur_stream_bytes_ptr += uword_size;
271
272
273    for (nameentry = nameentry_original;
274         nameentry != NULL; nameentry = nameentry->sne_next) {
275
276        /* Copy offset of die from start of compile-unit. */
277        WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
278                        (const void *) &nameentry->sne_die->di_offset,
279                        sizeof(nameentry->sne_die->di_offset),
280                        uword_size);
281        cur_stream_bytes_ptr += uword_size;
282
283        /* Copy the type name. */
284        strcpy((char *) cur_stream_bytes_ptr, nameentry->sne_name);
285        cur_stream_bytes_ptr += nameentry->sne_name_len + 1;
286    }
287
288    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
289                    (const void *) &big_zero,
290                    sizeof(big_zero), uword_size);
291
292
293
294
295    return (int) dbg->de_n_debug_sect;
296}
297