blob: c93c6ec0a316783af94a8547c6cb510009bbcb51 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Update section header.
Ulrich Drepperd7635782010-06-14 14:18:23 -07002 Copyright (C) 2000, 2001, 2002, 2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <gelf.h>
35#include <string.h>
36
37#include "libelfP.h"
38
39
40int
41gelf_update_shdr (Elf_Scn *scn, GElf_Shdr *src)
42{
43 int result = 0;
44 Elf *elf;
45
46 if (scn == NULL || src == NULL)
47 return 0;
48
49 elf = scn->elf;
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000050 rwlock_wrlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000051
52 if (elf->class == ELFCLASS32)
53 {
Ulrich Drepperd56e2322008-08-16 03:09:13 +000054 Elf32_Shdr *shdr
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000055 = scn->shdr.e32 ?: __elf32_getshdr_wrlock (scn);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000056
57 if (shdr == NULL)
58 {
59 __libelf_seterrno (ELF_E_INVALID_OPERAND);
60 goto out;
61 }
62
63 if (unlikely (src->sh_flags > 0xffffffffull)
64 || unlikely (src->sh_addr > 0xffffffffull)
65 || unlikely (src->sh_offset > 0xffffffffull)
66 || unlikely (src->sh_size > 0xffffffffull)
67 || unlikely (src->sh_addralign > 0xffffffffull)
68 || unlikely (src->sh_entsize > 0xffffffffull))
69 {
70 __libelf_seterrno (ELF_E_INVALID_DATA);
71 goto out;
72 }
73
74#define COPY(name) \
75 shdr->name = src->name
76 COPY (sh_name);
77 COPY (sh_type);
78 COPY (sh_flags);
79 COPY (sh_addr);
80 COPY (sh_offset);
81 COPY (sh_size);
82 COPY (sh_link);
83 COPY (sh_info);
84 COPY (sh_addralign);
85 COPY (sh_entsize);
86 }
87 else
88 {
Ulrich Drepperd56e2322008-08-16 03:09:13 +000089 Elf64_Shdr *shdr
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000090 = scn->shdr.e64 ?: __elf64_getshdr_wrlock (scn);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000091
92 if (shdr == NULL)
93 {
94 __libelf_seterrno (ELF_E_INVALID_OPERAND);
95 goto out;
96 }
97
98 /* We only have to copy the data. */
99 (void) memcpy (shdr, src, sizeof (GElf_Shdr));
100 }
101
Ulrich Drepperd7635782010-06-14 14:18:23 -0700102 /* Mark the section header as modified. */
103 scn->shdr_flags |= ELF_F_DIRTY;
104
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000105 result = 1;
106
107 out:
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000108 rwlock_unlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000109
110 return result;
111}