1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/* Test program for reading file with 64k+ sections and symbols.
Copyright (C) 2025 Mark J. Wielaard <mark@klomp.org>
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "system.h"
#include ELFUTILS_HEADER(elf)
#include <gelf.h>
/* Various fields (Elf_Half) can only contain up to 64k values. So
they need to be accessed through libelf functions which know where
the "true" value can be found. Also to get the section associated
with a symbol will need an extended symbol table entry. Use
elf_scnshndx to get at it and check we can get both symbol and
associated section names. */
static void
check_elf (const char *fname, bool use_mmap)
{
printf ("\nfname (use_mmap: %d): %s\n", use_mmap, fname);
int fd = open (fname, O_RDONLY);
if (fd == -1)
{
printf ("cannot open `%s': %s\n", fname, strerror (errno));
exit (1);
}
Elf *elf = elf_begin (fd, use_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
if (elf == NULL)
{
printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
exit (1);
}
/* How many sections are there? */
size_t shnum;
if (elf_getshdrnum (elf, &shnum) < 0)
{
printf ("elf_getshdrstrndx: %s\n", elf_errmsg (-1));
exit (1);
}
if (shnum < SHN_LORESERVE)
{
printf ("Not enough section for test, %zd < %d\n",
shnum, SHN_LORESERVE);
exit (1);
}
printf ("shnum: %zd\n", shnum);
/* Get the section that contains the section header names. Check it
can be found and it contains the substring 'str' in its own
name. */
size_t shstrndx;
if (elf_getshdrstrndx (elf, &shstrndx) < 0)
{
printf ("elf_getshdrstrndx: %s\n", elf_errmsg (-1));
exit (1);
}
Elf_Scn *scn = elf_getscn (elf, shstrndx);
if (scn == NULL)
{
printf ("cannot get section at index %zd: %s\n", shstrndx,
elf_errmsg (-1));
exit (1);
}
GElf_Shdr shdr_mem;
GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
if (shdr == NULL)
{
printf ("cannot get header for shstrndx section: %s\n",
elf_errmsg (-1));
exit (1);
}
/* Assume the section name contains at least the substring "str". */
const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
if (sname == NULL || strstr (sname, "str") == NULL)
{
printf ("Bad section name: %s\n", sname);
exit (1);
}
printf ("shstrndx section name: %s\n", sname);
/* The shstrndx section isn't a SYMTAB section, so elf_scnshndx will
return zero. */
int shndx = elf_scnshndx (scn);
if (shndx < 0)
{
printf ("elf_scnshndx error for shstrndx section: %s\n",
elf_errmsg (-1));
exit (1);
}
if (shndx > 0)
{
printf ("elf_scnshndx not zero for shstrndx section: %d\n", shndx);
exit (1);
}
/* Find the symtab and symtab_shndx by hand. */
size_t symtabndx = 0;
size_t symtabstrndx = 0;
size_t symtabsndx = 0;
size_t scns = 1; /* scn zero is skipped. */
Elf_Scn *symtabscn = NULL;
Elf_Scn *xndxscn = NULL;
scn = NULL;
while ((scn = elf_nextscn (elf, scn)) != NULL)
{
if (scns != elf_ndxscn (scn))
{
printf ("Unexpected elf_ndxscn %zd != %zd\n",
scns, elf_ndxscn (scn));
exit (1);
}
shdr = gelf_getshdr (scn, &shdr_mem);
if (shdr == NULL)
{
printf ("couldn't get shdr\n");
exit (1);
}
if (shdr->sh_type == SHT_SYMTAB)
{
symtabndx = elf_ndxscn (scn);
symtabstrndx = shdr->sh_link;
symtabscn = scn;
}
if (shdr->sh_type == SHT_SYMTAB_SHNDX)
{
symtabsndx = elf_ndxscn (scn);
xndxscn = scn;
}
/* We could break if we have both symtabndx and symtabsndx, but
we want to run through all sections to see if we get them
all and sanity check shnum. */
scns++;
}
printf ("scns: %zd\n", scns);
if (scns != shnum)
{
printf ("scns (%zd) != shnum (%zd)\n", scns, shnum);
exit (1);
}
printf ("symtabndx: %zd\n", symtabndx);
if (symtabndx == 0 || symtabscn == NULL)
{
printf ("No SYMTAB\n");
exit (1);
}
printf ("symtabsndx: %zd\n", symtabsndx);
if (symtabsndx == 0)
{
printf ("No SYMTAB_SNDX\n");
exit (1);
}
int scnshndx = elf_scnshndx (symtabscn);
printf ("scnshndx: %d\n", scnshndx);
if (scnshndx < 0)
{
printf ("elf_scnshndx failed: %s\n", elf_errmsg (-1));
exit (1);
}
if (scnshndx == 0)
{
printf ("elf_scnshndx couldn't find scnshndx, returned zero\n");
exit (1);
}
if ((size_t) scnshndx != symtabsndx)
{
printf ("elf_scnshndx found wrong scnshndx (%d),"
" should have been (%zd)\n", scnshndx, symtabsndx);
exit (1);
}
Elf_Data *symdata = elf_getdata (symtabscn, NULL);
if (symdata == NULL)
{
printf ("Couldn't elf_getdata for symtabscn: %s\n", elf_errmsg (-1));
exit (1);
}
size_t nsyms = symdata->d_size / (gelf_getclass (elf) == ELFCLASS32
? sizeof (Elf32_Sym)
: sizeof (Elf64_Sym));
Elf_Data *xndxdata = elf_getdata (xndxscn, NULL);
if (xndxdata == NULL)
{
printf ("Couldn't elf_getdata for xndxscn: %s\n", elf_errmsg (-1));
exit (1);
}
/* Now for every [yz]ddddd symbol , check that it matches the
section name (minus the starting dot). */
size_t yzsymcnt = 0;
for (size_t cnt = 0; cnt < nsyms; ++cnt)
{
const char *sym_name;
const char *section_name;
GElf_Word xndx;
GElf_Sym sym_mem;
GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, cnt,
&sym_mem, &xndx);
if (sym == NULL)
{
printf ("gelf_getsymshndx failed: %s\n", elf_errmsg (-1));
exit (1);
}
if (sym->st_shndx != SHN_XINDEX)
xndx = sym->st_shndx;
sym_name = elf_strptr (elf, symtabstrndx, sym->st_name);
if (sym_name == NULL)
{
printf ("elf_strptr returned NULL for sym %zd: %s\n",
cnt, elf_errmsg (-1));
exit (1);
}
scn = elf_getscn (elf, xndx);
if (scn == NULL)
{
printf ("cannot get section at index %d: %s\n", xndx,
elf_errmsg (-1));
exit (1);
}
shdr = gelf_getshdr (scn, &shdr_mem);
if (shdr == NULL)
{
printf ("cannot get header for sym xndx section: %s\n",
elf_errmsg (-1));
exit (1);
}
section_name = elf_strptr (elf, shstrndx, shdr->sh_name);
if (section_name == NULL)
{
printf ("elf_strptr returned NULL for section: %s\n",
elf_errmsg (-1));
exit (1);
}
if (GELF_ST_TYPE (sym->st_info) == STT_FUNC
&& (sym_name[0] == 'y' || sym_name[0] == 'z')
&& strlen (sym_name) == 6)
{
yzsymcnt++;
/* Every [yz]ddddd symbol comes from a section named after
the symbol prefixed with '.'. Except for powerpc ELFv1,
where all symbols point into the .opd. */
if ((section_name[0] != '.'
|| strcmp (sym_name, §ion_name[1]) != 0)
&& strcmp (section_name, ".opd") != 0)
{
printf ("BAD SYM/SECTION %zd sym: %s, section: %s\n",
cnt, sym_name, section_name);
exit (1);
}
}
}
#define YZSYMCNT (size_t) (2 * 8 * 8 * 8 * 8 * 8)
printf ("yzsymcnt: %zd\n", yzsymcnt);
if (yzsymcnt != YZSYMCNT)
{
printf ("Wrong number of yzsymcnts: %zd, should be %zd\n",
yzsymcnt, YZSYMCNT);
exit (1);
}
if (elf_end (elf) != 0)
{
printf ("failure in elf_end: %s\n", elf_errmsg (-1));
exit (1);
}
close (fd);
}
int
main (int argc, char *argv[] __attribute__ ((unused)))
{
elf_version (EV_CURRENT);
if (argc < 2)
{
printf ("Need at least one (file name) argument\n");
exit (1);
}
/* Test all file using ELF_C_READ and ELF_C_READ_MMAP. */
for (int i = 1; i < argc; i++)
{
check_elf (argv[i], false);
check_elf (argv[i], true);
}
return 0;
}
|