Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / test_elf.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16   Copyright (c) 2008 Eliot Dresselhaus
17
18   Permission is hereby granted, free of charge, to any person obtaining
19   a copy of this software and associated documentation files (the
20   "Software"), to deal in the Software without restriction, including
21   without limitation the rights to use, copy, modify, merge, publish,
22   distribute, sublicense, and/or sell copies of the Software, and to
23   permit persons to whom the Software is furnished to do so, subject to
24   the following conditions:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #include <vppinfra/elf.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43
44 #ifndef CLIB_UNIX
45 #error "unix only"
46 #endif
47
48 static clib_error_t * elf_set_interpreter (elf_main_t * em, char * interp)
49 {
50   elf_segment_t * g;
51   elf_section_t * s;
52   clib_error_t * error;
53
54   vec_foreach (g, em->segments)
55     {
56       if (g->header.type == ELF_SEGMENT_INTERP)
57         break;
58     }
59
60   if (g >= vec_end (em->segments))
61     return clib_error_return (0, "interpreter not found");
62
63   if (g->header.memory_size < 1 + strlen (interp))
64     return clib_error_return (0, "given interpreter does not fit; must be less than %d bytes (`%s' given)",
65                               g->header.memory_size, interp);
66
67   error = elf_get_section_by_start_address (em, g->header.virtual_address, &s);
68   if (error)
69     return error;
70
71   /* Put in new null terminated string. */
72   memset (s->contents, 0, vec_len (s->contents));
73   memcpy (s->contents, interp, strlen (interp));
74
75   return 0;
76 }
77
78 static void
79 delete_dynamic_rpath_entries_from_section (elf_main_t * em, elf_section_t * s)
80 {
81   elf64_dynamic_entry_t * e;
82   elf64_dynamic_entry_t * new_es = 0;
83
84   vec_foreach (e, em->dynamic_entries)
85     {
86       switch (e->type)
87         {
88         case ELF_DYNAMIC_ENTRY_RPATH:
89         case ELF_DYNAMIC_ENTRY_RUN_PATH:
90           break;
91
92         default:
93           vec_add1 (new_es, e[0]);
94           break;
95         }
96     }
97
98   /* Pad so as to keep section size constant. */
99   {
100     elf64_dynamic_entry_t e_end;
101     e_end.type = ELF_DYNAMIC_ENTRY_END;
102     e_end.data = 0;
103     while (vec_len (new_es) < vec_len (em->dynamic_entries))
104       vec_add1 (new_es, e_end);
105   }
106
107   elf_set_dynamic_entries (em);
108 }
109
110 static void elf_delete_dynamic_rpath_entries (elf_main_t * em)
111 {
112   elf_section_t * s;
113
114   vec_foreach (s, em->sections)
115     {
116       switch (s->header.type)
117         {
118         case ELF_SECTION_DYNAMIC:
119           delete_dynamic_rpath_entries_from_section (em, s);
120           break;
121
122         default:
123           break;
124         }
125     }
126 }
127
128 typedef struct {
129   elf_main_t elf_main;
130   char * input_file;
131   char * output_file;
132   char * set_interpreter;
133   int verbose;
134 } elf_test_main_t;
135
136 int main (int argc, char * argv[])
137 {
138   elf_test_main_t _tm, * tm = &_tm;
139   elf_main_t * em = &tm->elf_main;
140   unformat_input_t i;
141   clib_error_t * error = 0;
142
143   memset (tm, 0, sizeof (tm[0]));
144
145   unformat_init_command_line (&i, argv);
146   while (unformat_check_input (&i) != UNFORMAT_END_OF_INPUT)
147     {
148       if (unformat (&i, "in %s", &tm->input_file))
149         ;
150       else if (unformat (&i, "out %s", &tm->output_file))
151         ;
152       else if (unformat (&i, "set-interpreter %s", &tm->set_interpreter))
153         ;
154       else if (unformat (&i, "verbose"))
155         tm->verbose = ~0;
156       else if (unformat (&i, "verbose-symbols"))
157         tm->verbose |= FORMAT_ELF_MAIN_SYMBOLS;
158       else if (unformat (&i, "verbose-relocations"))
159         tm->verbose |= FORMAT_ELF_MAIN_RELOCATIONS;
160       else if (unformat (&i, "verbose-dynamic"))
161         tm->verbose |= FORMAT_ELF_MAIN_DYNAMIC;
162       else
163         {
164           error = unformat_parse_error (&i);
165           goto done;
166         }
167     }
168
169   if (! tm->input_file)
170     clib_error ("no input file");
171
172   error = elf_read_file (em, tm->input_file);
173   if (error)
174     goto done;
175
176   if (tm->set_interpreter)
177     {
178       clib_error_t * error = elf_set_interpreter (em, tm->set_interpreter);
179       if (error)
180         goto done;
181       elf_delete_dynamic_rpath_entries (em);
182     }
183
184   if (tm->verbose)
185     fformat (stdout, "%U", format_elf_main, em, tm->verbose);
186
187   if (tm->output_file)
188     error = elf_write_file (em, tm->output_file);
189
190   elf_main_free (em);
191
192  done:
193   if (error)
194     {
195       clib_error_report (error);
196       return 1;
197     }
198   else
199     return 0;
200 }