149d3bc91SRichard Lowe /*
207dc1947SRichard Lowe Copyright (C) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3*4d9fdb46SRobert Mustacchi Portions Copyright (C) 2009-2018 David Anderson. All Rights Reserved.
449d3bc91SRichard Lowe
549d3bc91SRichard Lowe This program is free software; you can redistribute it and/or modify it
6*4d9fdb46SRobert Mustacchi under the terms of version 2.1 of the GNU Lesser General Public License
749d3bc91SRichard Lowe as published by the Free Software Foundation.
849d3bc91SRichard Lowe
949d3bc91SRichard Lowe This program is distributed in the hope that it would be useful, but
1049d3bc91SRichard Lowe WITHOUT ANY WARRANTY; without even the implied warranty of
11*4d9fdb46SRobert Mustacchi MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1249d3bc91SRichard Lowe
1349d3bc91SRichard Lowe Further, this software is distributed without any warranty that it is
14*4d9fdb46SRobert Mustacchi free of the rightful claim of any third person regarding infringement
15*4d9fdb46SRobert Mustacchi or the like. Any license provided herein, whether implied or
1649d3bc91SRichard Lowe otherwise, applies only to this software file. Patent licenses, if
17*4d9fdb46SRobert Mustacchi any, provided herein do not apply to combinations of this program with
18*4d9fdb46SRobert Mustacchi other software, or any other product whatsoever.
1949d3bc91SRichard Lowe
20*4d9fdb46SRobert Mustacchi You should have received a copy of the GNU Lesser General Public
21*4d9fdb46SRobert Mustacchi License along with this program; if not, write the Free Software
2207dc1947SRichard Lowe Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
2349d3bc91SRichard Lowe USA.
2449d3bc91SRichard Lowe
2549d3bc91SRichard Lowe */
2649d3bc91SRichard Lowe
2749d3bc91SRichard Lowe #include "config.h"
2849d3bc91SRichard Lowe #include "dwarf_incl.h"
29*4d9fdb46SRobert Mustacchi #include "dwarf_error.h"
30*4d9fdb46SRobert Mustacchi #include "dwarf_util.h"
3149d3bc91SRichard Lowe
3249d3bc91SRichard Lowe int
dwarf_get_str(Dwarf_Debug dbg,Dwarf_Off offset,char ** string,Dwarf_Signed * returned_str_len,Dwarf_Error * error)3349d3bc91SRichard Lowe dwarf_get_str(Dwarf_Debug dbg,
34*4d9fdb46SRobert Mustacchi Dwarf_Off offset,
35*4d9fdb46SRobert Mustacchi char **string,
36*4d9fdb46SRobert Mustacchi Dwarf_Signed * returned_str_len, Dwarf_Error * error)
3749d3bc91SRichard Lowe {
3807dc1947SRichard Lowe int res = DW_DLV_ERROR;
39*4d9fdb46SRobert Mustacchi void *secptr = 0;
40*4d9fdb46SRobert Mustacchi void *begin = 0;
41*4d9fdb46SRobert Mustacchi void *end = 0;
4249d3bc91SRichard Lowe
4349d3bc91SRichard Lowe if (dbg == NULL) {
4407dc1947SRichard Lowe _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
4507dc1947SRichard Lowe return (DW_DLV_ERROR);
4649d3bc91SRichard Lowe }
4749d3bc91SRichard Lowe
4807dc1947SRichard Lowe if (offset == dbg->de_debug_str.dss_size) {
49*4d9fdb46SRobert Mustacchi /* Normal (if we've iterated thru the set of strings using
50*4d9fdb46SRobert Mustacchi dwarf_get_str and are at the end). */
5107dc1947SRichard Lowe return DW_DLV_NO_ENTRY;
5249d3bc91SRichard Lowe }
5307dc1947SRichard Lowe if (offset > dbg->de_debug_str.dss_size) {
5407dc1947SRichard Lowe _dwarf_error(dbg, error, DW_DLE_DEBUG_STR_OFFSET_BAD);
5507dc1947SRichard Lowe return (DW_DLV_ERROR);
5649d3bc91SRichard Lowe }
5749d3bc91SRichard Lowe
5849d3bc91SRichard Lowe if (string == NULL) {
5907dc1947SRichard Lowe _dwarf_error(dbg, error, DW_DLE_STRING_PTR_NULL);
6007dc1947SRichard Lowe return (DW_DLV_ERROR);
6149d3bc91SRichard Lowe }
6249d3bc91SRichard Lowe
6307dc1947SRichard Lowe res = _dwarf_load_section(dbg, &dbg->de_debug_str,error);
6449d3bc91SRichard Lowe if (res != DW_DLV_OK) {
6549d3bc91SRichard Lowe return res;
6649d3bc91SRichard Lowe }
67*4d9fdb46SRobert Mustacchi if (!dbg->de_debug_str.dss_size) {
68*4d9fdb46SRobert Mustacchi return (DW_DLV_NO_ENTRY);
69*4d9fdb46SRobert Mustacchi }
70*4d9fdb46SRobert Mustacchi secptr =dbg->de_debug_str.dss_data;
71*4d9fdb46SRobert Mustacchi begin = (char *)secptr + offset;
72*4d9fdb46SRobert Mustacchi end = (char *)secptr + dbg->de_debug_str.dss_size;
7349d3bc91SRichard Lowe
74*4d9fdb46SRobert Mustacchi res = _dwarf_check_string_valid(dbg,secptr,begin,end,
75*4d9fdb46SRobert Mustacchi DW_DLE_DEBUG_STR_OFFSET_BAD,error);
76*4d9fdb46SRobert Mustacchi if (res != DW_DLV_OK) {
77*4d9fdb46SRobert Mustacchi return res;
78*4d9fdb46SRobert Mustacchi }
7949d3bc91SRichard Lowe
80*4d9fdb46SRobert Mustacchi *string = (char *) begin;
81*4d9fdb46SRobert Mustacchi *returned_str_len = strlen(*string);
8249d3bc91SRichard Lowe return DW_DLV_OK;
8349d3bc91SRichard Lowe }
84