1 /*
2 * Copyright (c) 2003, 2016, 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 *
1233 static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
1234 int i = 0;
1235 ELF_PHDR* phbuf = NULL;
1236 ELF_PHDR* exec_php = NULL;
1237
1238 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL)
1239 return false;
1240
1241 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
1242 switch (exec_php->p_type) {
1243
1244 // add mappings for PT_LOAD segments
1245 case PT_LOAD: {
1246 // add only non-writable segments of non-zero filesz
1247 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
1248 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
1249 }
1250 break;
1251 }
1252
1253 // read the interpreter and it's segments
1254 case PT_INTERP: {
1255 char interp_name[BUF_SIZE];
1256
1257 pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset);
1258 print_debug("ELF interpreter %s\n", interp_name);
1259 // read interpreter segments as well
1260 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
1261 print_debug("can't open runtime loader\n");
1262 goto err;
1263 }
1264 break;
1265 }
1266
1267 // from PT_DYNAMIC we want to read address of first link_map addr
1268 case PT_DYNAMIC: {
1269 ph->core->dynamic_addr = exec_php->p_vaddr;
1270 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
1271 break;
1272 }
1273
1274 } // switch
1275 exec_php++;
1276 } // for
1277
|
1 /*
2 * Copyright (c) 2003, 2017, 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 *
1233 static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
1234 int i = 0;
1235 ELF_PHDR* phbuf = NULL;
1236 ELF_PHDR* exec_php = NULL;
1237
1238 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL)
1239 return false;
1240
1241 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
1242 switch (exec_php->p_type) {
1243
1244 // add mappings for PT_LOAD segments
1245 case PT_LOAD: {
1246 // add only non-writable segments of non-zero filesz
1247 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
1248 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
1249 }
1250 break;
1251 }
1252
1253 // read the interpreter and its segments
1254 case PT_INTERP: {
1255 ssize_t res;
1256 char interp_name[BUF_SIZE + 1];
1257
1258 // BUF_SIZE is PATH_MAX + NAME_MAX + 1.
1259 if (exec_php->p_filesz > BUF_SIZE) {
1260 goto err;
1261 }
1262 res = pread(ph->core->exec_fd, interp_name, exec_php->p_filesz, exec_php->p_offset);
1263 if (res < 0) {
1264 print_debug("couldn't read ELF interpreter name\n");
1265 goto err;
1266 }
1267 interp_name[exec_php->p_filesz] = '\0';
1268 print_debug("ELF interpreter %s\n", interp_name);
1269 // read interpreter segments as well
1270 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
1271 print_debug("can't open runtime loader\n");
1272 goto err;
1273 }
1274 break;
1275 }
1276
1277 // from PT_DYNAMIC we want to read address of first link_map addr
1278 case PT_DYNAMIC: {
1279 ph->core->dynamic_addr = exec_php->p_vaddr;
1280 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
1281 break;
1282 }
1283
1284 } // switch
1285 exec_php++;
1286 } // for
1287
|