| 236 | } |
| 237 | |
| 238 | static int |
| 239 | phdr_read(const struct buffer *in, struct parsed_elf *pelf, |
| 240 | struct xdr *xdr, int bit64) |
| 241 | { |
| 242 | struct buffer b; |
| 243 | Elf64_Phdr *phdr; |
| 244 | Elf64_Ehdr *ehdr; |
| 245 | int i; |
| 246 | |
| 247 | ehdr = &pelf->ehdr; |
| 248 | /* cons up an input buffer for the headers. |
| 249 | * Note that the program headers can be anywhere, |
| 250 | * per the ELF spec, You'd be surprised how many ELF |
| 251 | * readers miss this little detail. |
| 252 | */ |
| 253 | buffer_splice(&b, in, ehdr->e_phoff, |
| 254 | (uint32_t)ehdr->e_phentsize * ehdr->e_phnum); |
| 255 | if (check_size(in, ehdr->e_phoff, buffer_size(&b), "program headers")) |
| 256 | return -1; |
| 257 | |
| 258 | /* gather up all the phdrs. |
| 259 | * We do them all at once because there is more |
| 260 | * than one loop over all the phdrs. |
| 261 | */ |
| 262 | phdr = calloc(ehdr->e_phnum, sizeof(*phdr)); |
| 263 | for (i = 0; i < ehdr->e_phnum; i++) { |
| 264 | DEBUG("Parsing segment %d\n", i); |
| 265 | elf_phdr(&b, &phdr[i], ehdr->e_phentsize, xdr, bit64); |
| 266 | |
| 267 | /* Ensure the contents are valid within the elf file. */ |
| 268 | if (check_size(in, phdr[i].p_offset, phdr[i].p_filesz, |
| 269 | "segment contents")) { |
| 270 | free(phdr); |
| 271 | return -1; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | pelf->phdr = phdr; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int |
| 281 | shdr_read(const struct buffer *in, struct parsed_elf *pelf, |
no test coverage detected