2024-01-01 18:12:26 +00:00
|
|
|
/* Copyright (C) 1999-2024 Free Software Foundation, Inc.
|
1999-12-04 08:00:00 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
2005-12-07 05:49:17 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
2007-07-16 00:56:07 +00:00
|
|
|
it under the terms of the GNU General Public License as published
|
|
|
|
by the Free Software Foundation; version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
1999-12-04 08:00:00 +00:00
|
|
|
|
2005-12-07 05:49:17 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
1999-12-04 08:00:00 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2005-12-07 05:49:17 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
1999-12-04 08:00:00 +00:00
|
|
|
|
2005-12-07 05:49:17 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
along with this program; if not, see <https://www.gnu.org/licenses/>. */
|
1999-12-04 08:00:00 +00:00
|
|
|
|
|
|
|
/* The code in this file and in readelflib is a heavily simplified
|
|
|
|
version of the readelf program that's part of the current binutils
|
|
|
|
development version. Besides the simplification, it has also been
|
|
|
|
modified to read some other file formats. */
|
|
|
|
|
2003-07-22 21:35:06 +00:00
|
|
|
#include <a.out.h>
|
1999-12-04 08:00:00 +00:00
|
|
|
#include <elf.h>
|
|
|
|
#include <error.h>
|
|
|
|
#include <libintl.h>
|
2003-07-22 21:35:06 +00:00
|
|
|
#include <link.h>
|
1999-12-04 08:00:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2003-07-22 21:35:06 +00:00
|
|
|
#include <sys/param.h>
|
1999-12-04 08:00:00 +00:00
|
|
|
#include <sys/stat.h>
|
2000-05-26 10:23:45 +00:00
|
|
|
#include <gnu/lib-names.h>
|
1999-12-04 08:00:00 +00:00
|
|
|
|
2005-12-21 22:16:34 +00:00
|
|
|
#include <ldconfig.h>
|
2024-05-06 17:16:32 +00:00
|
|
|
#include <endswith.h>
|
1999-12-04 08:00:00 +00:00
|
|
|
|
|
|
|
#define Elf32_CLASS ELFCLASS32
|
|
|
|
#define Elf64_CLASS ELFCLASS64
|
|
|
|
|
|
|
|
struct known_names
|
|
|
|
{
|
|
|
|
const char *soname;
|
|
|
|
int flag;
|
|
|
|
};
|
|
|
|
|
2024-05-06 17:16:33 +00:00
|
|
|
/* Check if string corresponds to a GDB extension file. */
|
2015-06-24 10:21:29 +00:00
|
|
|
static bool
|
2024-05-06 17:16:33 +00:00
|
|
|
is_gdb_extension_file (const char *name)
|
2015-06-24 10:21:29 +00:00
|
|
|
{
|
|
|
|
size_t len = strlen (name);
|
2024-05-06 17:16:33 +00:00
|
|
|
return (endswithn (name, len, "-gdb.gdb")
|
|
|
|
|| endswithn (name, len, "-gdb.py")
|
|
|
|
|| endswithn (name, len, "-gdb.scm"));
|
2015-06-24 10:21:29 +00:00
|
|
|
}
|
1999-12-04 08:00:00 +00:00
|
|
|
|
|
|
|
/* Returns 0 if everything is ok, != 0 in case of error. */
|
|
|
|
int
|
2000-09-30 00:54:42 +00:00
|
|
|
process_file (const char *real_file_name, const char *file_name,
|
2022-03-04 13:27:15 +00:00
|
|
|
const char *lib, int *flag, unsigned int *isa_level,
|
|
|
|
char **soname, int is_link, struct stat *stat_buf)
|
1999-12-04 08:00:00 +00:00
|
|
|
{
|
|
|
|
FILE *file;
|
2021-12-29 13:20:46 +00:00
|
|
|
struct stat statbuf;
|
1999-12-04 08:00:00 +00:00
|
|
|
void *file_contents;
|
|
|
|
int ret;
|
|
|
|
ElfW(Ehdr) *elf_header;
|
|
|
|
struct exec *aout_header;
|
|
|
|
|
|
|
|
ret = 0;
|
2022-07-14 17:37:22 +00:00
|
|
|
/* Just set FLAG_ELF_LIBC6 as old formats are not supported anymore. */
|
|
|
|
*flag = FLAG_ELF_LIBC6;
|
1999-12-04 08:00:00 +00:00
|
|
|
*soname = NULL;
|
|
|
|
|
2000-09-30 00:54:42 +00:00
|
|
|
file = fopen (real_file_name, "rb");
|
1999-12-04 08:00:00 +00:00
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
/* No error for stale symlink. */
|
2000-09-16 23:00:38 +00:00
|
|
|
if (is_link && strstr (file_name, ".so") != NULL)
|
1999-12-04 08:00:00 +00:00
|
|
|
return 1;
|
|
|
|
error (0, 0, _("Input file %s not found.\n"), file_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:20:46 +00:00
|
|
|
if (fstat (fileno (file), &statbuf) < 0)
|
1999-12-04 08:00:00 +00:00
|
|
|
{
|
|
|
|
error (0, 0, _("Cannot fstat file %s.\n"), file_name);
|
2000-04-11 16:27:38 +00:00
|
|
|
fclose (file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the file is large enough so that we can access the
|
|
|
|
information. We're only checking the size of the headers here. */
|
2002-08-25 04:51:08 +00:00
|
|
|
if ((size_t) statbuf.st_size < sizeof (struct exec)
|
|
|
|
|| (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
|
2000-04-11 16:27:38 +00:00
|
|
|
{
|
2004-09-26 13:39:25 +00:00
|
|
|
if (statbuf.st_size == 0)
|
|
|
|
error (0, 0, _("File %s is empty, not checked."), file_name);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[SELFMAG];
|
|
|
|
size_t n = MIN (statbuf.st_size, SELFMAG);
|
|
|
|
if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0)
|
|
|
|
error (0, 0, _("File %s is too small, not checked."), file_name);
|
|
|
|
}
|
2000-04-11 16:27:38 +00:00
|
|
|
fclose (file);
|
1999-12-04 08:00:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-04-11 16:27:38 +00:00
|
|
|
file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
|
|
|
|
fileno (file), 0);
|
1999-12-04 08:00:00 +00:00
|
|
|
if (file_contents == MAP_FAILED)
|
|
|
|
{
|
|
|
|
error (0, 0, _("Cannot mmap file %s.\n"), file_name);
|
|
|
|
fclose (file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First check if this is an aout file. */
|
|
|
|
aout_header = (struct exec *) file_contents;
|
|
|
|
if (N_MAGIC (*aout_header) == ZMAGIC
|
2002-09-05 18:52:18 +00:00
|
|
|
#ifdef QMAGIC /* Linuxism. */
|
|
|
|
|| N_MAGIC (*aout_header) == QMAGIC
|
|
|
|
#endif
|
|
|
|
)
|
1999-12-04 08:00:00 +00:00
|
|
|
{
|
2000-04-11 16:27:38 +00:00
|
|
|
/* Aout files don't have a soname, just return the name
|
2007-08-01 Andreas Jaeger <aj@suse.de>
Jakub Jelinek <jakub@redhat.com>
* elf/ldconfig.c (opt_ignore_aux_cache): Add new option.
(options): Add option.
(parse_opt): Handle option.
(manual_link): Adjust process_file caller. Call implicit_soname.
(search_dir): Formatting. Use and populate auxiliary cache.
(main): Load and save auxiliary cache.
* elf/readlib.c (process_file): Add stat_buf argument. Pass struct
stat64 from fstat64 to caller.
(implicit_soname): New function.
* elf/readelflib.c (process_elf_file): If DT_SONAME is not present,
leave *soname as NULL.
* elf/cache.c: Include libgen.h.
(print_entry, print_cache, compare, save_cache, add_to_cache):
Formatting and cleanups.
(aux_cache_entry_id, aux_cache_entry, aux_cache_file_entry,
aux_cache_file): New structures.
(AUX_CACHEMAGIC): Define.
(primes): New array.
(aux_hash_size, aux_hash): New variables.
(aux_cache_entry_id_hash, nextprime, init_aux_cache,
search_aux_cache, insert_to_aux_cache, add_to_aux_cache,
load_aux_cache, save_aux_cache): New functions.
* sysdeps/generic/ldconfig.h (_PATH_LDCONFIG_AUX_CACHE): Define.
(init_aux_cache, search_aux_cache, add_to_aux_cache,
load_aux_cache, save_aux_cache, implicit_soname): New prototypes.
(process_file): Adjust prototype.
2007-08-12 20:09:16 +00:00
|
|
|
including the major number. */
|
1999-12-04 08:00:00 +00:00
|
|
|
char *copy, *major, *dot;
|
|
|
|
copy = xstrdup (lib);
|
|
|
|
major = strstr (copy, ".so.");
|
|
|
|
if (major)
|
|
|
|
{
|
|
|
|
dot = strstr (major + 4, ".");
|
|
|
|
if (dot)
|
|
|
|
*dot = '\0';
|
|
|
|
}
|
|
|
|
*soname = copy;
|
|
|
|
goto done;
|
|
|
|
}
|
1999-12-09 01:39:16 +00:00
|
|
|
|
1999-12-04 08:00:00 +00:00
|
|
|
elf_header = (ElfW(Ehdr) *) file_contents;
|
Update.
2001-03-12 Jakub Jelinek <jakub@redhat.com>
* csu/Makefile (abi-tag.h): Define OS and version separately, allow
version to be overriden from config.h.
* csu/abi-note.S: Use OS and version separately, include config.h.
* elf/dl-load.c (_dl_osversion): New.
(_dl_map_object_from_fd): Kill some warnings.
(open_verify): Check .note.ABI-tag of the library if present.
* elf/Makefile (CPPFLAGS-dl-load.c): Add -I$(csu-objpfx).
* elf/cache.c (struct cache_entry): Add osversion.
(print_entry): Print osversion.
(print_cache): Pass osversion to it.
(compare): Sort according to osversion.
(save_cache): Set osversion.
(add_to_cache): Add osversion argument.
* sysdeps/generic/ldconfig.h (add_to_cache, process_file,
process_elf_file): Add osversion argument.
* elf/readlib.c (process_file): Likewise.
* sysdeps/generic/readelflib.c (process_elf_file): Likewise.
* sysdeps/unix/sysv/linux/ia64/readelflib.c (process_elf_file,
process_elf32_file, process_elf64_file): Likewise.
* sysdeps/unix/sysv/linux/i386/readelflib.c (process_elf_file,
process_elf32_file, process_elf64_file): Likewise.
* sysdeps/unix/sysv/linux/sparc/readelflib.c (process_elf_file,
process_elf32_file, process_elf64_file): Likewise.
* elf/ldconfig.c (manual_link): Pass it.
(search_dir): Issue diagnostic if two libs with the same soname in
the same directory have different .note.ABI-tag. Record osversion in
dlib_entry and use it from there.
(struct lib_entry): Remove.
(struct dlib_entry): Add osversion.
* sysdeps/generic/dl-cache.c (_dl_load_cache_lookup): Check
osversion.
* sysdeps/generic/dl-cache.h (struct file_entry_new): Replace __unused
field with osversion.
* sysdeps/generic/ldsodefs.h (_dl_osversion): Declare.
* sysdeps/unix/sysv/linux/init-first.c: Include ldsodefs.h.
* sysdeps/unix/sysv/linux/dl-osinfo.h (DL_SYSDEP_OSCHECK): Save kernel
version in _dl_osversion.
* sysdeps/unix/sysv/linux/configure.in: Define __ABI_TAG_VERSION.
* Makerules (build-shlib-helper, build-module-helper): New.
(build-shlib, build-module-helper): Make sure .note.ABI-tag comes
early.
* config.h.in (__ABI_TAG_VERSION): Add.
* elf/dl-minimal.c (__strtoul_internal): Set endptr on return.
* sysdeps/unix/sysv/linux/i386/dl-librecon.h (EXTRA_LD_ENVVARS):
Handle LD_ASSUME_KERNEL.
* sysdeps/unix/sysv/linux/dl-librecon.h: New.
2001-03-16 07:40:05 +00:00
|
|
|
if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0)
|
1999-12-04 08:00:00 +00:00
|
|
|
{
|
2003-07-22 21:35:06 +00:00
|
|
|
/* The file is neither ELF nor aout. Check if it's a linker
|
|
|
|
script, like libc.so - otherwise complain. Only search the
|
|
|
|
beginning of the file. */
|
|
|
|
size_t len = MIN (statbuf.st_size, 512);
|
1999-12-04 08:00:00 +00:00
|
|
|
if (memmem (file_contents, len, "GROUP", 5) == NULL
|
2015-06-24 10:21:29 +00:00
|
|
|
&& memmem (file_contents, len, "GNU ld script", 13) == NULL
|
2024-05-06 17:16:33 +00:00
|
|
|
&& !is_gdb_extension_file (file_name))
|
1999-12-04 08:00:00 +00:00
|
|
|
error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
|
|
|
|
file_name);
|
|
|
|
ret = 1;
|
|
|
|
}
|
2003-07-22 20:00:48 +00:00
|
|
|
/* Libraries have to be shared object files. */
|
|
|
|
else if (elf_header->e_type != ET_DYN)
|
|
|
|
ret = 1;
|
2022-03-04 13:27:15 +00:00
|
|
|
else if (process_elf_file (file_name, lib, flag, isa_level, soname,
|
|
|
|
file_contents, statbuf.st_size))
|
1999-12-04 08:00:00 +00:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* Clean up allocated memory and resources. */
|
|
|
|
munmap (file_contents, statbuf.st_size);
|
|
|
|
fclose (file);
|
|
|
|
|
2007-08-01 Andreas Jaeger <aj@suse.de>
Jakub Jelinek <jakub@redhat.com>
* elf/ldconfig.c (opt_ignore_aux_cache): Add new option.
(options): Add option.
(parse_opt): Handle option.
(manual_link): Adjust process_file caller. Call implicit_soname.
(search_dir): Formatting. Use and populate auxiliary cache.
(main): Load and save auxiliary cache.
* elf/readlib.c (process_file): Add stat_buf argument. Pass struct
stat64 from fstat64 to caller.
(implicit_soname): New function.
* elf/readelflib.c (process_elf_file): If DT_SONAME is not present,
leave *soname as NULL.
* elf/cache.c: Include libgen.h.
(print_entry, print_cache, compare, save_cache, add_to_cache):
Formatting and cleanups.
(aux_cache_entry_id, aux_cache_entry, aux_cache_file_entry,
aux_cache_file): New structures.
(AUX_CACHEMAGIC): Define.
(primes): New array.
(aux_hash_size, aux_hash): New variables.
(aux_cache_entry_id_hash, nextprime, init_aux_cache,
search_aux_cache, insert_to_aux_cache, add_to_aux_cache,
load_aux_cache, save_aux_cache): New functions.
* sysdeps/generic/ldconfig.h (_PATH_LDCONFIG_AUX_CACHE): Define.
(init_aux_cache, search_aux_cache, add_to_aux_cache,
load_aux_cache, save_aux_cache, implicit_soname): New prototypes.
(process_file): Adjust prototype.
2007-08-12 20:09:16 +00:00
|
|
|
*stat_buf = statbuf;
|
1999-12-04 08:00:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get architecture specific version of process_elf_file. */
|
2005-12-21 15:31:50 +00:00
|
|
|
#include <readelflib.c>
|