elf_data.c (b626f5a73a48f44a31a200291b141e1da408a2ff) elf_data.c (b6b6f9cc7c36b4ff856e2cee50cb825d82a329fe)
1/*-
2 * Copyright (c) 2006,2008,2011 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27#include <assert.h>
28#include <errno.h>
29#include <libelf.h>
30#include <stdint.h>
31#include <stdlib.h>
32
33#include "_libelf.h"
34
1/*-
2 * Copyright (c) 2006,2008,2011 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27#include <assert.h>
28#include <errno.h>
29#include <libelf.h>
30#include <stdint.h>
31#include <stdlib.h>
32
33#include "_libelf.h"
34
35ELFTC_VCSID("$Id: elf_data.c 3258 2015-11-20 18:59:43Z emaste $");
35ELFTC_VCSID("$Id: elf_data.c 3466 2016-05-11 18:35:44Z emaste $");
36
37Elf_Data *
38elf_getdata(Elf_Scn *s, Elf_Data *ed)
39{
40 Elf *e;
41 unsigned int sh_type;
42 int elfclass, elftype;
43 size_t count, fsz, msz;

--- 45 unchanged lines hidden (view full) ---

89
90 if (sh_type == SHT_NULL) {
91 LIBELF_SET_ERROR(SECTION, 0);
92 return (NULL);
93 }
94
95 if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
96 elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
36
37Elf_Data *
38elf_getdata(Elf_Scn *s, Elf_Data *ed)
39{
40 Elf *e;
41 unsigned int sh_type;
42 int elfclass, elftype;
43 size_t count, fsz, msz;

--- 45 unchanged lines hidden (view full) ---

89
90 if (sh_type == SHT_NULL) {
91 LIBELF_SET_ERROR(SECTION, 0);
92 return (NULL);
93 }
94
95 if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
96 elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
97 sh_offset + sh_size > (uint64_t) e->e_rawsize)) {
97 (sh_offset > e->e_rawsize || sh_size > e->e_rawsize - sh_offset))) {
98 LIBELF_SET_ERROR(SECTION, 0);
99 return (NULL);
100 }
101
102 if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
103 (elftype, (size_t) 1, e->e_version)) == 0) {
104 LIBELF_SET_ERROR(UNIMPL, 0);
105 return (NULL);

--- 143 unchanged lines hidden (view full) ---

249 }
250
251 if (sh_type == SHT_NULL) {
252 LIBELF_SET_ERROR(SECTION, 0);
253 return (NULL);
254 }
255
256 if (sh_type != SHT_NOBITS &&
98 LIBELF_SET_ERROR(SECTION, 0);
99 return (NULL);
100 }
101
102 if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
103 (elftype, (size_t) 1, e->e_version)) == 0) {
104 LIBELF_SET_ERROR(UNIMPL, 0);
105 return (NULL);

--- 143 unchanged lines hidden (view full) ---

249 }
250
251 if (sh_type == SHT_NULL) {
252 LIBELF_SET_ERROR(SECTION, 0);
253 return (NULL);
254 }
255
256 if (sh_type != SHT_NOBITS &&
257 sh_offset + sh_size > (uint64_t) e->e_rawsize) {
257 (sh_offset > e->e_rawsize || sh_size > e->e_rawsize - sh_offset)) {
258 LIBELF_SET_ERROR(SECTION, 0);
259 return (NULL);
260 }
261
262 if ((d = _libelf_allocate_data(s)) == NULL)
263 return (NULL);
264
265 d->d_data.d_buf = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
266 e->e_rawfile + sh_offset;
267 d->d_data.d_off = 0;
268 d->d_data.d_align = sh_align;
269 d->d_data.d_size = sh_size;
270 d->d_data.d_type = ELF_T_BYTE;
271 d->d_data.d_version = e->e_version;
272
273 STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
274
275 return (&d->d_data);
276}
258 LIBELF_SET_ERROR(SECTION, 0);
259 return (NULL);
260 }
261
262 if ((d = _libelf_allocate_data(s)) == NULL)
263 return (NULL);
264
265 d->d_data.d_buf = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
266 e->e_rawfile + sh_offset;
267 d->d_data.d_off = 0;
268 d->d_data.d_align = sh_align;
269 d->d_data.d_size = sh_size;
270 d->d_data.d_type = ELF_T_BYTE;
271 d->d_data.d_version = e->e_version;
272
273 STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
274
275 return (&d->d_data);
276}