Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 1 | /* Conversion functions for notes. |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 2 | Copyright (C) 2007, 2009, 2014, 2018 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 | static void |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 30 | elf_cvt_note (void *dest, const void *src, size_t len, int encode, |
| 31 | bool nhdr8) |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 32 | { |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 33 | /* Note that the header is always the same size, but the padding |
| 34 | differs for GNU Property notes. */ |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 35 | assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr)); |
| 36 | |
Roland McGrath | 315ebd5 | 2009-01-05 00:10:11 -0800 | [diff] [blame] | 37 | while (len >= sizeof (Elf32_Nhdr)) |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 38 | { |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 39 | /* Convert the header. */ |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 40 | (1 ? Elf32_cvt_Nhdr : Elf64_cvt_Nhdr) (dest, src, sizeof (Elf32_Nhdr), |
| 41 | encode); |
| 42 | const Elf32_Nhdr *n = encode ? src : dest; |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 43 | |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 44 | size_t note_len = sizeof *n; |
| 45 | |
| 46 | /* desc needs to be aligned. */ |
| 47 | note_len += n->n_namesz; |
| 48 | note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len); |
Mark Wielaard | e65d91d | 2019-01-16 12:25:57 +0100 | [diff] [blame] | 49 | if (note_len > len || note_len < sizeof *n) |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 50 | break; |
| 51 | |
| 52 | /* data as a whole needs to be aligned. */ |
| 53 | note_len += n->n_descsz; |
| 54 | note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len); |
Mark Wielaard | e65d91d | 2019-01-16 12:25:57 +0100 | [diff] [blame] | 55 | if (note_len > len || note_len < sizeof *n) |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 56 | break; |
| 57 | |
| 58 | /* Copy or skip the note data. */ |
| 59 | size_t note_data_len = note_len - sizeof *n; |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 60 | src += sizeof *n; |
| 61 | dest += sizeof *n; |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 62 | if (src != dest) |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 63 | memcpy (dest, src, note_data_len); |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 64 | |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 65 | src += note_data_len; |
| 66 | dest += note_data_len; |
| 67 | len -= note_len; |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 68 | } |
Mark Wielaard | 51abc74 | 2014-11-23 15:27:23 +0100 | [diff] [blame] | 69 | |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 70 | /* Copy over any leftover data unconverted. Probably part of |
Mark Wielaard | 51abc74 | 2014-11-23 15:27:23 +0100 | [diff] [blame] | 71 | truncated name/desc data. */ |
| 72 | if (unlikely (len > 0) && src != dest) |
| 73 | memcpy (dest, src, len); |
Roland McGrath | c76f0b0 | 2007-09-27 07:31:33 +0000 | [diff] [blame] | 74 | } |
Mark Wielaard | 5199e15 | 2018-10-15 23:35:47 +0200 | [diff] [blame] | 75 | |
| 76 | static void |
| 77 | elf_cvt_note4 (void *dest, const void *src, size_t len, int encode) |
| 78 | { |
| 79 | elf_cvt_note (dest, src, len, encode, false); |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | elf_cvt_note8 (void *dest, const void *src, size_t len, int encode) |
| 84 | { |
| 85 | elf_cvt_note (dest, src, len, encode, true); |
| 86 | } |