Fix grouping and reuse other locales in various locales
[glibc.git] / elf / check-execstack.c
blob96e481208de7e7589b064a2cd74374b33c518242
1 /* Check for executable stacks in DSOs.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contribute by Ulrich Drepper <drepper@redhat.com>. 2009.
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>
29 #include "check-execstack.h"
32 #ifdef BITS
34 # define AB(name) _AB (name, BITS)
35 # define _AB(name, bits) __AB (name, bits)
36 # define __AB(name, bits) name##bits
37 # define E(name) _E (name, BITS)
38 # define _E(name, bits) __E (name, bits)
39 # define __E(name, bits) Elf##bits##_##name
40 # define SWAP(val) \
41 ({ __typeof (val) __res; \
42 if (((ehdr.e_ident[EI_DATA] == ELFDATA2MSB \
43 && BYTE_ORDER == LITTLE_ENDIAN) \
44 || (ehdr.e_ident[EI_DATA] == ELFDATA2LSB \
45 && BYTE_ORDER == BIG_ENDIAN)) \
46 && sizeof (val) != 1) \
47 { \
48 if (sizeof (val) == 2) \
49 __res = bswap_16 (val); \
50 else if (sizeof (val) == 4) \
51 __res = bswap_32 (val); \
52 else \
53 __res = bswap_64 (val); \
54 } \
55 else \
56 __res = (val); \
57 __res; })
60 static int
61 AB(handle_file) (const char *fname, int fd)
63 E(Ehdr) ehdr;
65 if (pread (fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr))
67 read_error:
68 printf ("%s: read error: %m\n", fname);
69 return 1;
72 const size_t phnum = SWAP (ehdr.e_phnum);
73 const size_t phentsize = SWAP (ehdr.e_phentsize);
75 /* Read the program header. */
76 E(Phdr) *phdr = alloca (phentsize * phnum);
77 if (pread (fd, phdr, phentsize * phnum, SWAP (ehdr.e_phoff))
78 != phentsize * phnum)
79 goto read_error;
81 /* Search for the PT_GNU_STACK entry. */
82 for (size_t cnt = 0; cnt < phnum; ++cnt)
83 if (SWAP (phdr[cnt].p_type) == PT_GNU_STACK)
85 unsigned int flags = SWAP(phdr[cnt].p_flags);
86 if (flags & PF_X)
88 printf ("%s: executable stack signaled\n", fname);
89 return 1;
92 return 0;
95 if (DEFAULT_STACK_PERMS & PF_X)
97 printf ("%s: no PT_GNU_STACK entry\n", fname);
98 return 1;
101 return 0;
104 # undef BITS
105 #else
107 # define BITS 32
108 # include "check-execstack.c"
110 # define BITS 64
111 # include "check-execstack.c"
114 static int
115 handle_file (const char *fname)
117 int fd = open (fname, O_RDONLY);
118 if (fd == -1)
120 printf ("cannot open %s: %m\n", fname);
121 return 1;
124 /* Read was is supposed to be the ELF header. Read the initial
125 bytes to determine whether this is a 32 or 64 bit file. */
126 char ident[EI_NIDENT];
127 if (read (fd, ident, EI_NIDENT) != EI_NIDENT)
129 printf ("%s: read error: %m\n", fname);
130 close (fd);
131 return 1;
134 if (memcmp (&ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
136 printf ("%s: not an ELF file\n", fname);
137 close (fd);
138 return 1;
141 int result;
142 if (ident[EI_CLASS] == ELFCLASS64)
143 result = handle_file64 (fname, fd);
144 else
145 result = handle_file32 (fname, fd);
147 close (fd);
149 return result;
154 main (int argc, char *argv[])
156 int cnt;
157 int result = 0;
159 for (cnt = 1; cnt < argc; ++cnt)
160 result |= handle_file (argv[cnt]);
161 return result;
163 #endif