1*f8fb3368SJohn Marino /*-
2*f8fb3368SJohn Marino * Copyright (c) 2006,2008 Joseph Koshy
3*f8fb3368SJohn Marino * All rights reserved.
4*f8fb3368SJohn Marino *
5*f8fb3368SJohn Marino * Redistribution and use in source and binary forms, with or without
6*f8fb3368SJohn Marino * modification, are permitted provided that the following conditions
7*f8fb3368SJohn Marino * are met:
8*f8fb3368SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*f8fb3368SJohn Marino * notice, this list of conditions and the following disclaimer.
10*f8fb3368SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*f8fb3368SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*f8fb3368SJohn Marino * documentation and/or other materials provided with the distribution.
13*f8fb3368SJohn Marino *
14*f8fb3368SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*f8fb3368SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*f8fb3368SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*f8fb3368SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*f8fb3368SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*f8fb3368SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*f8fb3368SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*f8fb3368SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*f8fb3368SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*f8fb3368SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*f8fb3368SJohn Marino * SUCH DAMAGE.
25*f8fb3368SJohn Marino */
26*f8fb3368SJohn Marino
27*f8fb3368SJohn Marino #include <assert.h>
28*f8fb3368SJohn Marino #include <gelf.h>
29*f8fb3368SJohn Marino #include <libelf.h>
30*f8fb3368SJohn Marino #include <limits.h>
31*f8fb3368SJohn Marino #include <stdint.h>
32*f8fb3368SJohn Marino
33*f8fb3368SJohn Marino #include "_libelf.h"
34*f8fb3368SJohn Marino
35*f8fb3368SJohn Marino ELFTC_VCSID("$Id: gelf_shdr.c 3177 2015-03-30 18:19:41Z emaste $");
36*f8fb3368SJohn Marino
37*f8fb3368SJohn Marino Elf32_Shdr *
elf32_getshdr(Elf_Scn * s)38*f8fb3368SJohn Marino elf32_getshdr(Elf_Scn *s)
39*f8fb3368SJohn Marino {
40*f8fb3368SJohn Marino return (_libelf_getshdr(s, ELFCLASS32));
41*f8fb3368SJohn Marino }
42*f8fb3368SJohn Marino
43*f8fb3368SJohn Marino Elf64_Shdr *
elf64_getshdr(Elf_Scn * s)44*f8fb3368SJohn Marino elf64_getshdr(Elf_Scn *s)
45*f8fb3368SJohn Marino {
46*f8fb3368SJohn Marino return (_libelf_getshdr(s, ELFCLASS64));
47*f8fb3368SJohn Marino }
48*f8fb3368SJohn Marino
49*f8fb3368SJohn Marino GElf_Shdr *
gelf_getshdr(Elf_Scn * s,GElf_Shdr * d)50*f8fb3368SJohn Marino gelf_getshdr(Elf_Scn *s, GElf_Shdr *d)
51*f8fb3368SJohn Marino {
52*f8fb3368SJohn Marino int ec;
53*f8fb3368SJohn Marino void *sh;
54*f8fb3368SJohn Marino Elf32_Shdr *sh32;
55*f8fb3368SJohn Marino Elf64_Shdr *sh64;
56*f8fb3368SJohn Marino
57*f8fb3368SJohn Marino if (d == NULL) {
58*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
59*f8fb3368SJohn Marino return (NULL);
60*f8fb3368SJohn Marino }
61*f8fb3368SJohn Marino
62*f8fb3368SJohn Marino if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL)
63*f8fb3368SJohn Marino return (NULL);
64*f8fb3368SJohn Marino
65*f8fb3368SJohn Marino ec = s->s_elf->e_class;
66*f8fb3368SJohn Marino assert(ec == ELFCLASS32 || ec == ELFCLASS64);
67*f8fb3368SJohn Marino
68*f8fb3368SJohn Marino if (ec == ELFCLASS32) {
69*f8fb3368SJohn Marino sh32 = (Elf32_Shdr *) sh;
70*f8fb3368SJohn Marino
71*f8fb3368SJohn Marino d->sh_name = sh32->sh_name;
72*f8fb3368SJohn Marino d->sh_type = sh32->sh_type;
73*f8fb3368SJohn Marino d->sh_flags = (Elf64_Xword) sh32->sh_flags;
74*f8fb3368SJohn Marino d->sh_addr = (Elf64_Addr) sh32->sh_addr;
75*f8fb3368SJohn Marino d->sh_offset = (Elf64_Off) sh32->sh_offset;
76*f8fb3368SJohn Marino d->sh_size = (Elf64_Xword) sh32->sh_size;
77*f8fb3368SJohn Marino d->sh_link = sh32->sh_link;
78*f8fb3368SJohn Marino d->sh_info = sh32->sh_info;
79*f8fb3368SJohn Marino d->sh_addralign = (Elf64_Xword) sh32->sh_addralign;
80*f8fb3368SJohn Marino d->sh_entsize = (Elf64_Xword) sh32->sh_entsize;
81*f8fb3368SJohn Marino } else {
82*f8fb3368SJohn Marino sh64 = (Elf64_Shdr *) sh;
83*f8fb3368SJohn Marino *d = *sh64;
84*f8fb3368SJohn Marino }
85*f8fb3368SJohn Marino
86*f8fb3368SJohn Marino return (d);
87*f8fb3368SJohn Marino }
88*f8fb3368SJohn Marino
89*f8fb3368SJohn Marino int
gelf_update_shdr(Elf_Scn * scn,GElf_Shdr * s)90*f8fb3368SJohn Marino gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s)
91*f8fb3368SJohn Marino {
92*f8fb3368SJohn Marino int ec;
93*f8fb3368SJohn Marino Elf *e;
94*f8fb3368SJohn Marino Elf32_Shdr *sh32;
95*f8fb3368SJohn Marino
96*f8fb3368SJohn Marino
97*f8fb3368SJohn Marino if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL ||
98*f8fb3368SJohn Marino e->e_kind != ELF_K_ELF ||
99*f8fb3368SJohn Marino ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
100*f8fb3368SJohn Marino LIBELF_SET_ERROR(ARGUMENT, 0);
101*f8fb3368SJohn Marino return (0);
102*f8fb3368SJohn Marino }
103*f8fb3368SJohn Marino
104*f8fb3368SJohn Marino if (e->e_cmd == ELF_C_READ) {
105*f8fb3368SJohn Marino LIBELF_SET_ERROR(MODE, 0);
106*f8fb3368SJohn Marino return (0);
107*f8fb3368SJohn Marino }
108*f8fb3368SJohn Marino
109*f8fb3368SJohn Marino (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
110*f8fb3368SJohn Marino
111*f8fb3368SJohn Marino if (ec == ELFCLASS64) {
112*f8fb3368SJohn Marino scn->s_shdr.s_shdr64 = *s;
113*f8fb3368SJohn Marino return (1);
114*f8fb3368SJohn Marino }
115*f8fb3368SJohn Marino
116*f8fb3368SJohn Marino sh32 = &scn->s_shdr.s_shdr32;
117*f8fb3368SJohn Marino
118*f8fb3368SJohn Marino sh32->sh_name = s->sh_name;
119*f8fb3368SJohn Marino sh32->sh_type = s->sh_type;
120*f8fb3368SJohn Marino LIBELF_COPY_U32(sh32, s, sh_flags);
121*f8fb3368SJohn Marino LIBELF_COPY_U32(sh32, s, sh_addr);
122*f8fb3368SJohn Marino LIBELF_COPY_U32(sh32, s, sh_offset);
123*f8fb3368SJohn Marino LIBELF_COPY_U32(sh32, s, sh_size);
124*f8fb3368SJohn Marino sh32->sh_link = s->sh_link;
125*f8fb3368SJohn Marino sh32->sh_info = s->sh_info;
126*f8fb3368SJohn Marino LIBELF_COPY_U32(sh32, s, sh_addralign);
127*f8fb3368SJohn Marino LIBELF_COPY_U32(sh32, s, sh_entsize);
128*f8fb3368SJohn Marino
129*f8fb3368SJohn Marino return (1);
130*f8fb3368SJohn Marino }
131