initializer syntax to C99.
[glibc/pb-stable.git] / elf / check-textrel.c
blob69210b2be8c429fcea8e9f7e374dd632acf3c66f
1 /* Check for text relocations in DSOs.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contribute by Ulrich Drepper <drepper@redhat.com>. 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <byteswap.h>
22 #include <elf.h>
23 #include <endian.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
31 #ifdef BITS
33 # define AB(name) _AB (name, BITS)
34 # define _AB(name, bits) __AB (name, bits)
35 # define __AB(name, bits) name##bits
36 # define E(name) _E (name, BITS)
37 # define _E(name, bits) __E (name, bits)
38 # define __E(name, bits) Elf##bits##_##name
39 # define SWAP(val) \
40 ({ __typeof (val) __res; \
41 if ((ehdr.e_ident[EI_DATA] == ELFDATA2MSB && BYTE_ORDER == LITTLE_ENDIAN \
42 || ehdr.e_ident[EI_DATA] == ELFDATA2LSB && BYTE_ORDER == BIG_ENDIAN)\
43 && sizeof (val) != 1) \
44 { \
45 if (sizeof (val) == 2) \
46 __res = bswap_16 (val); \
47 else if (sizeof (val) == 4) \
48 __res = bswap_32 (val); \
49 else \
50 __res = bswap_64 (val); \
51 } \
52 else \
53 __res = (val); \
54 __res; })
57 static int
58 AB(handle_file) (const char *fname, int fd)
60 E(Ehdr) ehdr;
62 if (pread (fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr))
64 read_error:
65 printf ("%s: read error: %m\n", fname);
66 return 1;
69 const size_t phnum = SWAP (ehdr.e_phnum);
70 const size_t phentsize = SWAP (ehdr.e_phentsize);
72 /* Read the program header. */
73 E(Phdr) *phdr = alloca (phentsize * phnum);
74 if (pread (fd, phdr, phentsize * phnum, SWAP (ehdr.e_phoff))
75 != phentsize * phnum)
76 goto read_error;
78 /* Search for the PT_DYNAMIC entry. */
79 size_t cnt;
80 for (cnt = 0; cnt < phnum; ++cnt)
81 if (SWAP (phdr[cnt].p_type) == PT_DYNAMIC)
82 break;
84 if (cnt == phnum)
86 printf ("%s: no DYNAMIC segment found\n", fname);
87 return 1;
90 /* Read the dynamic segment. */
91 size_t pmemsz = SWAP(phdr[cnt].p_memsz);
92 E(Dyn) *dyn = alloca (pmemsz);
93 if (pread (fd, dyn, pmemsz, SWAP(phdr[cnt].p_offset)) != pmemsz)
94 goto read_error;
96 /* Search for an DT_TEXTREL entry of DT_FLAGS with the DF_TEXTREL
97 bit set. */
98 for (cnt = 0; (cnt + 1) * sizeof (E(Dyn)) - 1 < pmemsz; ++cnt)
100 unsigned int tag = SWAP (dyn[cnt].d_tag);
102 if (tag == DT_NULL)
103 /* We reached the end. */
104 break;
106 if (tag == DT_TEXTREL
107 || (tag == DT_FLAGS
108 && (SWAP (dyn[cnt].d_un.d_val) & DF_TEXTREL) != 0))
110 /* Urgh! The DSO has text relocations. */
111 printf ("%s: text relocations used\n", fname);
112 return 1;
116 printf ("%s: OK\n", fname);
118 return 0;
121 # undef BITS
122 #else
124 # define BITS 32
125 # include "check-textrel.c"
127 # define BITS 64
128 # include "check-textrel.c"
131 static int
132 handle_file (const char *fname)
134 int fd = open (fname, O_RDONLY);
135 if (fd == -1)
137 printf ("cannot open %s: %m\n", fname);
138 return 1;
141 /* Read was is supposed to be the ELF header. Read the initial
142 bytes to determine whether this is a 32 or 64 bit file. */
143 char ident[EI_NIDENT];
144 if (read (fd, ident, EI_NIDENT) != EI_NIDENT)
146 printf ("%s: read error: %m\n", fname);
147 close (fd);
148 return 1;
151 if (memcmp (&ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
153 printf ("%s: not an ELF file\n", fname);
154 close (fd);
155 return 1;
158 int result;
159 if (ident[EI_CLASS] == ELFCLASS64)
160 result = handle_file64 (fname, fd);
161 else
162 result = handle_file32 (fname, fd);
164 close (fd);
166 return result;
171 main (int argc, char *argv[])
173 int cnt;
174 int result = 0;
176 for (cnt = 1; cnt < argc; ++cnt)
177 result |= handle_file (argv[cnt]);
179 return result;
181 #endif