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 *
806 int i = 0;
807 ELF_PHDR* phbuf = NULL;
808 ELF_PHDR* exec_php = NULL;
809
810 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) {
811 return false;
812 }
813
814 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
815 switch (exec_php->p_type) {
816
817 // add mappings for PT_LOAD segments
818 case PT_LOAD: {
819 // add only non-writable segments of non-zero filesz
820 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
821 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
822 }
823 break;
824 }
825
826 // read the interpreter and it's segments
827 case PT_INTERP: {
828 char interp_name[BUF_SIZE + 1];
829
830 // BUF_SIZE is PATH_MAX + NAME_MAX + 1.
831 if (exec_php->p_filesz > BUF_SIZE) {
832 goto err;
833 }
834 pread(ph->core->exec_fd, interp_name, exec_php->p_filesz, exec_php->p_offset);
835 interp_name[exec_php->p_filesz] = '\0';
836 print_debug("ELF interpreter %s\n", interp_name);
837 // read interpreter segments as well
838 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
839 print_debug("can't open runtime loader\n");
840 goto err;
841 }
842 break;
843 }
844
845 // from PT_DYNAMIC we want to read address of first link_map addr
846 case PT_DYNAMIC: {
847 if (exec_ehdr->e_type == ET_EXEC) {
848 ph->core->dynamic_addr = exec_php->p_vaddr;
849 } else { // ET_DYN
850 // dynamic_addr has entry point of executable.
851 // Thus we should substract it.
852 ph->core->dynamic_addr += exec_php->p_vaddr - exec_ehdr->e_entry;
853 }
854 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
|
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 *
806 int i = 0;
807 ELF_PHDR* phbuf = NULL;
808 ELF_PHDR* exec_php = NULL;
809
810 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) {
811 return false;
812 }
813
814 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
815 switch (exec_php->p_type) {
816
817 // add mappings for PT_LOAD segments
818 case PT_LOAD: {
819 // add only non-writable segments of non-zero filesz
820 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
821 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
822 }
823 break;
824 }
825
826 // read the interpreter and its segments
827 case PT_INTERP: {
828 ssize_t res;
829 char interp_name[BUF_SIZE + 1];
830
831 // BUF_SIZE is PATH_MAX + NAME_MAX + 1.
832 if (exec_php->p_filesz > BUF_SIZE) {
833 goto err;
834 }
835 res = pread(ph->core->exec_fd, interp_name, exec_php->p_filesz, exec_php->p_offset);
836 if (res < 0) {
837 print_debug("couldn't read ELF interpreter name\n");
838 goto err;
839 }
840 interp_name[exec_php->p_filesz] = '\0';
841 print_debug("ELF interpreter %s\n", interp_name);
842 // read interpreter segments as well
843 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
844 print_debug("can't open runtime loader\n");
845 goto err;
846 }
847 break;
848 }
849
850 // from PT_DYNAMIC we want to read address of first link_map addr
851 case PT_DYNAMIC: {
852 if (exec_ehdr->e_type == ET_EXEC) {
853 ph->core->dynamic_addr = exec_php->p_vaddr;
854 } else { // ET_DYN
855 // dynamic_addr has entry point of executable.
856 // Thus we should substract it.
857 ph->core->dynamic_addr += exec_php->p_vaddr - exec_ehdr->e_entry;
858 }
859 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
|