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  *
  23  */
  24 
  25 #include "salibelf.h"
  26 #include <stdlib.h>
  27 #include <unistd.h>
  28 #include <string.h>
  29 
  30 extern void print_debug(const char*,...);
  31 
  32 // ELF file parsing helpers. Note that we do *not* use libelf here.
  33 int read_elf_header(int fd, ELF_EHDR* ehdr) {
  34    if (pread(fd, ehdr, sizeof (ELF_EHDR), 0) != sizeof (ELF_EHDR) ||
  35             memcmp(&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 ||
  36             ehdr->e_version != EV_CURRENT) {
  37         return 0;
  38    }
  39    return 1;
  40 }
  41 
  42 bool is_elf_file(int fd) {
  43    ELF_EHDR ehdr;
  44    return read_elf_header(fd, &ehdr);
  45 }
  46 
  47 // read program header table of an ELF file
  48 ELF_PHDR* read_program_header_table(int fd, ELF_EHDR* hdr) {
  49    ELF_PHDR* phbuf = 0;
  50    // allocate memory for program header table
  51    size_t nbytes = hdr->e_phnum * hdr->e_phentsize;
  52 
  53    if ((phbuf = (ELF_PHDR*) malloc(nbytes)) == NULL) {
  54       print_debug("can't allocate memory for reading program header table\n");
  55       return NULL;
  56    }
  57 
  58    if (pread(fd, phbuf, nbytes, hdr->e_phoff) != nbytes) {
  59       print_debug("ELF file is truncated! can't read program header table\n");
  60       free(phbuf);
  61       return NULL;
  62    }
  63 
  64    return phbuf;
  65 }
  66 
  67 // read section header table of an ELF file
  68 ELF_SHDR* read_section_header_table(int fd, ELF_EHDR* hdr) {
  69    ELF_SHDR* shbuf = 0;
  70    // allocate memory for section header table
  71    size_t nbytes = hdr->e_shnum * hdr->e_shentsize;
  72 
  73    if ((shbuf = (ELF_SHDR*) malloc(nbytes)) == NULL) {
  74       print_debug("can't allocate memory for reading section header table\n");
  75       return NULL;
  76    }
  77 
  78    if (pread(fd, shbuf, nbytes, hdr->e_shoff) != nbytes) {
  79       print_debug("ELF file is truncated! can't read section header table\n");
  80       free(shbuf);
  81       return NULL;
  82    }
  83 
  84    return shbuf;
  85 }
  86 
  87 // read a particular section's data
  88 void* read_section_data(int fd, ELF_EHDR* ehdr, ELF_SHDR* shdr) {
  89   void *buf = NULL;
  90   if (shdr->sh_type == SHT_NOBITS || shdr->sh_size == 0) {
  91      return buf;
  92   }
  93   if ((buf = calloc(shdr->sh_size, 1)) == NULL) {
  94      print_debug("can't allocate memory for reading section data\n");
  95      return NULL;
  96   }
  97   if (pread(fd, buf, shdr->sh_size, shdr->sh_offset) != shdr->sh_size) {
  98      free(buf);
  99      print_debug("section data read failed\n");
 100      return NULL;
 101   }
 102   return buf;
 103 }
 104 
 105 uintptr_t find_base_address(int fd, ELF_EHDR* ehdr) {
 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 }