xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_stringsection.c (revision 08855964b9970604433f7b19dcd71cf5af5e5f14)
1 /*
2   Copyright (C) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3   Portions Copyright (C) 2009-2018 David Anderson. 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 */
26 
27 #include "config.h"
28 #include "dwarf_incl.h"
29 #include "dwarf_error.h"
30 #include "dwarf_util.h"
31 
32 int
33 dwarf_get_str(Dwarf_Debug dbg,
34    Dwarf_Off offset,
35    char **string,
36    Dwarf_Signed * returned_str_len, Dwarf_Error * error)
37 {
38     int res = DW_DLV_ERROR;
39     void *secptr = 0;
40     void *begin = 0;
41     void *end = 0;
42 
43     if (dbg == NULL) {
44         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
45         return (DW_DLV_ERROR);
46     }
47 
48     if (offset == dbg->de_debug_str.dss_size) {
49         /*  Normal (if we've iterated thru the set of strings using
50             dwarf_get_str and are at the end). */
51         return DW_DLV_NO_ENTRY;
52     }
53     if (offset > dbg->de_debug_str.dss_size) {
54         _dwarf_error(dbg, error, DW_DLE_DEBUG_STR_OFFSET_BAD);
55         return (DW_DLV_ERROR);
56     }
57 
58     if (string == NULL) {
59         _dwarf_error(dbg, error, DW_DLE_STRING_PTR_NULL);
60         return (DW_DLV_ERROR);
61     }
62 
63     res = _dwarf_load_section(dbg, &dbg->de_debug_str,error);
64     if (res != DW_DLV_OK) {
65         return res;
66     }
67     if (!dbg->de_debug_str.dss_size) {
68         return (DW_DLV_NO_ENTRY);
69     }
70     secptr =dbg->de_debug_str.dss_data;
71     begin = (char *)secptr + offset;
72     end =   (char *)secptr + dbg->de_debug_str.dss_size;
73 
74     res = _dwarf_check_string_valid(dbg,secptr,begin,end,
75         DW_DLE_DEBUG_STR_OFFSET_BAD,error);
76     if (res != DW_DLV_OK) {
77         return res;
78     }
79 
80     *string = (char *) begin;
81     *returned_str_len = strlen(*string);
82     return DW_DLV_OK;
83 }
84