1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
106 uintptr_t baseaddr = (uintptr_t)-1;
107 int cnt;
108 ELF_PHDR *phbuf, *phdr;
109
110 // read program header table
111 if ((phbuf = read_program_header_table(fd, ehdr)) == NULL) {
112 goto quit;
113 }
114
115 // the base address of a shared object is the lowest vaddr of
116 // its loadable segments (PT_LOAD)
117 for (phdr = phbuf, cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) {
118 if (phdr->p_type == PT_LOAD && phdr->p_vaddr < baseaddr) {
119 baseaddr = phdr->p_vaddr;
120 }
121 }
122
123 quit:
124 if (phbuf) free(phbuf);
125 return baseaddr;
126 }
|
1 /*
2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
106 uintptr_t baseaddr = (uintptr_t)-1;
107 int cnt;
108 ELF_PHDR *phbuf, *phdr;
109
110 // read program header table
111 if ((phbuf = read_program_header_table(fd, ehdr)) == NULL) {
112 goto quit;
113 }
114
115 // the base address of a shared object is the lowest vaddr of
116 // its loadable segments (PT_LOAD)
117 for (phdr = phbuf, cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) {
118 if (phdr->p_type == PT_LOAD && phdr->p_vaddr < baseaddr) {
119 baseaddr = phdr->p_vaddr;
120 }
121 }
122
123 quit:
124 if (phbuf) free(phbuf);
125 return baseaddr;
126 }
127
128 struct elf_section *find_section_by_name(char *name,
129 int fd,
130 ELF_EHDR *ehdr,
131 struct elf_section *scn_cache) {
132 char *strtab;
133 int cnt;
134 int strtab_size;
135
136 // Section cache have to already contain data for e_shstrndx section.
137 // If it's not true - elf file is broken, so just bail out
138 if (scn_cache[ehdr->e_shstrndx].c_data == NULL) {
139 return NULL;
140 }
141
142 strtab = scn_cache[ehdr->e_shstrndx].c_data;
143 strtab_size = scn_cache[ehdr->e_shstrndx].c_shdr->sh_size;
144
145 for (cnt = 0; cnt < ehdr->e_shnum; ++cnt) {
146 if (scn_cache[cnt].c_shdr->sh_name < strtab_size) {
147 if (strcmp(scn_cache[cnt].c_shdr->sh_name + strtab, name) == 0) {
148 scn_cache[cnt].c_data = read_section_data(fd, ehdr, scn_cache[cnt].c_shdr);
149 return &scn_cache[cnt];
150 }
151 }
152 }
153
154 return NULL;
155 }
|