Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 1 | /* Get note information at the supplied offset. |
Mark Wielaard | df2fe50 | 2014-11-14 17:05:08 +0100 | [diff] [blame] | 2 | Copyright (C) 2007, 2014 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 8 | * the GNU Lesser General Public License as published by the Free |
| 9 | Software Foundation; either version 3 of the License, or (at |
| 10 | your option) any later version |
| 11 | |
| 12 | or |
| 13 | |
| 14 | * the GNU General Public License as published by the Free |
| 15 | Software Foundation; either version 2 of the License, or (at |
| 16 | your option) any later version |
| 17 | |
| 18 | or both in parallel, as here. |
| 19 | |
| 20 | elfutils is distributed in the hope that it will be useful, but |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 25 | You should have received copies of the GNU General Public License and |
| 26 | the GNU Lesser General Public License along with this program. If |
| 27 | not, see <http://www.gnu.org/licenses/>. */ |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | # include <config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <assert.h> |
| 34 | #include <gelf.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include "libelfP.h" |
| 38 | |
| 39 | size_t |
| 40 | gelf_getnote (data, offset, result, name_offset, desc_offset) |
| 41 | Elf_Data *data; |
| 42 | size_t offset; |
| 43 | GElf_Nhdr *result; |
| 44 | size_t *name_offset; |
| 45 | size_t *desc_offset; |
| 46 | { |
| 47 | if (data == NULL) |
| 48 | return 0; |
| 49 | |
| 50 | if (unlikely (data->d_type != ELF_T_NHDR)) |
| 51 | { |
| 52 | __libelf_seterrno (ELF_E_INVALID_HANDLE); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* It's easy to handle this type. It has the same size for 32 and |
| 57 | 64 bit objects. */ |
| 58 | assert (sizeof (GElf_Nhdr) == sizeof (Elf32_Nhdr)); |
| 59 | assert (sizeof (GElf_Nhdr) == sizeof (Elf64_Nhdr)); |
| 60 | |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 61 | rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock); |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 62 | |
| 63 | /* The data is already in the correct form. Just make sure the |
| 64 | offset is OK. */ |
Mark Wielaard | df2fe50 | 2014-11-14 17:05:08 +0100 | [diff] [blame] | 65 | if (unlikely (offset > data->d_size |
| 66 | || data->d_size - offset < sizeof (GElf_Nhdr))) |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 67 | { |
| 68 | __libelf_seterrno (ELF_E_OFFSET_RANGE); |
| 69 | offset = 0; |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | const GElf_Nhdr *n = data->d_buf + offset; |
| 74 | offset += sizeof *n; |
| 75 | |
Mark Wielaard | 8ea90b7 | 2014-11-16 11:30:51 +0100 | [diff] [blame] | 76 | /* Include padding. Check below for overflow. */ |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 77 | GElf_Word namesz = NOTE_ALIGN (n->n_namesz); |
| 78 | GElf_Word descsz = NOTE_ALIGN (n->n_descsz); |
| 79 | |
Mark Wielaard | 8ea90b7 | 2014-11-16 11:30:51 +0100 | [diff] [blame] | 80 | if (unlikely (offset > data->d_size |
| 81 | || data->d_size - offset < namesz |
| 82 | || (namesz == 0 && n->n_namesz != 0))) |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 83 | offset = 0; |
| 84 | else |
| 85 | { |
| 86 | *name_offset = offset; |
| 87 | offset += namesz; |
Mark Wielaard | 8ea90b7 | 2014-11-16 11:30:51 +0100 | [diff] [blame] | 88 | if (unlikely (offset > data->d_size |
| 89 | || data->d_size - offset < descsz |
| 90 | || (descsz == 0 && n->n_descsz != 0))) |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 91 | offset = 0; |
| 92 | else |
| 93 | { |
| 94 | *desc_offset = offset; |
| 95 | offset += descsz; |
| 96 | *result = *n; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 101 | rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock); |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 102 | |
| 103 | return offset; |
| 104 | } |