1 |
commit: 7fcf66c9ab50111f5574dbce8cd52d6fd77ad699 |
2 |
Author: Anthony G. Basile <blueness <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Feb 9 21:49:46 2013 +0000 |
4 |
Commit: Anthony G. Basile <blueness <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon May 20 19:53:55 2013 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=7fcf66c9 |
7 |
|
8 |
misc/remove-ptpax.c: code to convert PT_PAX_FLAGS to PT_NULL phdr |
9 |
|
10 |
--- |
11 |
misc/remove-ptpax.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ |
12 |
1 files changed, 103 insertions(+), 0 deletions(-) |
13 |
|
14 |
diff --git a/misc/remove-ptpax.c b/misc/remove-ptpax.c |
15 |
new file mode 100644 |
16 |
index 0000000..ba441a5 |
17 |
--- /dev/null |
18 |
+++ b/misc/remove-ptpax.c |
19 |
@@ -0,0 +1,103 @@ |
20 |
+/* |
21 |
+ remove-ptpax.c: this file is part of the elfix package |
22 |
+ Copyright (C) 2013 Anthony G. Basile |
23 |
+ |
24 |
+ This program is free software: you can redistribute it and/or modify |
25 |
+ it under the terms of the GNU General Public License as published by |
26 |
+ the Free Software Foundation, either version 3 of the License, or |
27 |
+ (at your option) any later version. |
28 |
+ |
29 |
+ This program is distributed in the hope that it will be useful, |
30 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
31 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
32 |
+ GNU General Public License for more details. |
33 |
+ |
34 |
+ You should have received a copy of the GNU General Public License |
35 |
+ along with this program. If not, see <http://www.gnu.org/licenses/>. |
36 |
+*/ |
37 |
+ |
38 |
+#include <stdio.h> |
39 |
+#include <stdlib.h> |
40 |
+#include <sys/types.h> |
41 |
+#include <sys/stat.h> |
42 |
+#include <fcntl.h> |
43 |
+#include <gelf.h> |
44 |
+ |
45 |
+#ifndef PT_PAX_FLAGS |
46 |
+ #define PT_PAX_FLAGS 0x65041580 |
47 |
+#endif |
48 |
+ |
49 |
+int |
50 |
+remove_ptpax(int fd) |
51 |
+{ |
52 |
+ Elf *elf; |
53 |
+ GElf_Phdr phdr; |
54 |
+ size_t i, phnum; |
55 |
+ |
56 |
+ if(elf_version(EV_CURRENT) == EV_NONE) |
57 |
+ { |
58 |
+ printf("\tELF ERROR: Library out of date.\n"); |
59 |
+ return EXIT_FAILURE; |
60 |
+ } |
61 |
+ |
62 |
+ if((elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL)) == NULL) |
63 |
+ { |
64 |
+ printf("\tELF ERROR: elf_begin() fail: %s\n", elf_errmsg(elf_errno())); |
65 |
+ return EXIT_FAILURE; |
66 |
+ } |
67 |
+ |
68 |
+ if(elf_kind(elf) != ELF_K_ELF) |
69 |
+ { |
70 |
+ elf_end(elf); |
71 |
+ printf("\tELF ERROR: elf_kind() fail: this is not an elf file.\n"); |
72 |
+ return EXIT_FAILURE; |
73 |
+ } |
74 |
+ |
75 |
+ elf_getphdrnum(elf, &phnum); |
76 |
+ |
77 |
+ for(i=0; i<phnum; i++) |
78 |
+ { |
79 |
+ if(gelf_getphdr(elf, i, &phdr) != &phdr) |
80 |
+ { |
81 |
+ elf_end(elf); |
82 |
+ printf("\tELF ERROR: gelf_getphdr(): %s\n", elf_errmsg(elf_errno())); |
83 |
+ return EXIT_FAILURE; |
84 |
+ } |
85 |
+ |
86 |
+ if(phdr.p_type == PT_PAX_FLAGS) |
87 |
+ { |
88 |
+ phdr.p_type = PT_NULL; |
89 |
+ if(!gelf_update_phdr(elf, i, &phdr)) |
90 |
+ { |
91 |
+ elf_end(elf); |
92 |
+ printf("\tELF ERROR: gelf_update_phdr(): %s", elf_errmsg(elf_errno())); |
93 |
+ return EXIT_FAILURE; |
94 |
+ } |
95 |
+ } |
96 |
+ } |
97 |
+ |
98 |
+ elf_end(elf); |
99 |
+ return EXIT_SUCCESS; |
100 |
+} |
101 |
+ |
102 |
+ |
103 |
+int |
104 |
+main( int argc, char *argv[]) |
105 |
+{ |
106 |
+ int fd; |
107 |
+ |
108 |
+ if(argc != 2) |
109 |
+ { |
110 |
+ fprintf(stderr, "Usage: %s <filename>\n", argv[0]) ; |
111 |
+ exit(EXIT_SUCCESS); |
112 |
+ } |
113 |
+ |
114 |
+ if((fd = open(argv[1], O_RDWR)) < 0) |
115 |
+ { |
116 |
+ printf("\topen(O_RDWR) failed: cannot change PT_PAX flags\n"); |
117 |
+ exit(EXIT_FAILURE); |
118 |
+ } |
119 |
+ else |
120 |
+ exit(remove_ptpax(fd)); |
121 |
+ |
122 |
+} |