Imported Upstream version 0.153
[platform/upstream/elfutils.git] / libelf / elf32_updatenull.c
1 /* Update data structures for changes.
2    Copyright (C) 2000-2010 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
6    Red Hat elfutils is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by the
8    Free Software Foundation; version 2 of the License.
9
10    Red Hat elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with Red Hat elfutils; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19    In addition, as a special exception, Red Hat, Inc. gives You the
20    additional right to link the code of Red Hat elfutils with code licensed
21    under any Open Source Initiative certified open source license
22    (http://www.opensource.org/licenses/index.php) which requires the
23    distribution of source code with any binary distribution and to
24    distribute linked combinations of the two.  Non-GPL Code permitted under
25    this exception must only link to the code of Red Hat elfutils through
26    those well defined interfaces identified in the file named EXCEPTION
27    found in the source code files (the "Approved Interfaces").  The files
28    of Non-GPL Code may instantiate templates or use macros or inline
29    functions from the Approved Interfaces without causing the resulting
30    work to be covered by the GNU General Public License.  Only Red Hat,
31    Inc. may make changes or additions to the list of Approved Interfaces.
32    Red Hat's grant of this exception is conditioned upon your not adding
33    any new exceptions.  If you wish to add a new Approved Interface or
34    exception, please contact Red Hat.  You must obey the GNU General Public
35    License in all respects for all of the Red Hat elfutils code and other
36    code used in conjunction with Red Hat elfutils except the Non-GPL Code
37    covered by this exception.  If you modify this file, you may extend this
38    exception to your version of the file, but you are not obligated to do
39    so.  If you do not wish to provide this exception without modification,
40    you must delete this exception statement from your version and license
41    this file solely under the GPL without exception.
42
43    Red Hat elfutils is an included package of the Open Invention Network.
44    An included package of the Open Invention Network is a package for which
45    Open Invention Network licensees cross-license their patents.  No patent
46    license is granted, either expressly or impliedly, by designation as an
47    included package.  Should you wish to participate in the Open Invention
48    Network licensing program, please visit www.openinventionnetwork.com
49    <http://www.openinventionnetwork.com>.  */
50
51 #ifdef HAVE_CONFIG_H
52 # include <config.h>
53 #endif
54
55 #include <assert.h>
56 #include <endian.h>
57 #include <libelf.h>
58 #include <stdbool.h>
59 #include <string.h>
60 #include <sys/param.h>
61
62 #include "libelfP.h"
63 #include "elf-knowledge.h"
64
65 #ifndef LIBELFBITS
66 # define LIBELFBITS 32
67 #endif
68
69
70
71 static int
72 ELFW(default_ehdr,LIBELFBITS) (Elf *elf, ElfW2(LIBELFBITS,Ehdr) *ehdr,
73                                size_t shnum, int *change_bop)
74 {
75   /* Always write the magic bytes.  */
76   if (memcmp (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
77     {
78       memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG);
79       elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
80     }
81
82   /* Always set the file class.  */
83   update_if_changed (ehdr->e_ident[EI_CLASS], ELFW(ELFCLASS,LIBELFBITS),
84                      elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
85
86   /* Set the data encoding if necessary.  */
87   if (unlikely (ehdr->e_ident[EI_DATA] == ELFDATANONE))
88     {
89       ehdr->e_ident[EI_DATA] =
90         BYTE_ORDER == BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB;
91       elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
92     }
93   else if (unlikely (ehdr->e_ident[EI_DATA] >= ELFDATANUM))
94     {
95       __libelf_seterrno (ELF_E_DATA_ENCODING);
96       return 1;
97     }
98   else
99     *change_bop = ((BYTE_ORDER == LITTLE_ENDIAN
100                     && ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
101                    || (BYTE_ORDER == BIG_ENDIAN
102                        && ehdr->e_ident[EI_DATA] != ELFDATA2MSB));
103
104   /* Unconditionally overwrite the ELF version.  */
105   update_if_changed (ehdr->e_ident[EI_VERSION], EV_CURRENT,
106                      elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
107
108   if (unlikely (ehdr->e_version == EV_NONE)
109       || unlikely (ehdr->e_version >= EV_NUM))
110     {
111       __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
112       return 1;
113     }
114
115   if (unlikely (shnum >= SHN_LORESERVE))
116     {
117       update_if_changed (ehdr->e_shnum, 0,
118                          elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
119     }
120   else
121     update_if_changed (ehdr->e_shnum, shnum,
122                        elf->state.ELFW(elf,LIBELFBITS).ehdr_flags);
123
124   if (unlikely (ehdr->e_ehsize != elf_typesize (LIBELFBITS, ELF_T_EHDR, 1)))
125     {
126       ehdr->e_ehsize = elf_typesize (LIBELFBITS, ELF_T_EHDR, 1);
127       elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
128     }
129
130   return 0;
131 }
132
133
134 off_t
135 internal_function
136 __elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
137 {
138   ElfW2(LIBELFBITS,Ehdr) *ehdr;
139   int changed = 0;
140   int ehdr_flags = 0;
141
142   ehdr = __elfw2(LIBELFBITS,getehdr_wrlock) (elf);
143
144   /* Set the default values.  */
145   if (ELFW(default_ehdr,LIBELFBITS) (elf, ehdr, shnum, change_bop) != 0)
146     return -1;
147
148   /* At least the ELF header is there.  */
149   off_t size = elf_typesize (LIBELFBITS, ELF_T_EHDR, 1);
150
151   /* Set the program header position.  */
152   if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL
153       && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN
154           || ehdr->e_type == ET_CORE))
155     (void) __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
156   if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
157     {
158       /* Only executables, shared objects, and core files have a program
159          header.  */
160       if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN
161           && unlikely (ehdr->e_type != ET_CORE))
162         {
163           __libelf_seterrno (ELF_E_INVALID_PHDR);
164           return -1;
165         }
166
167       size_t phnum;
168       if (unlikely (__elf_getphdrnum_rdlock (elf, &phnum) != 0))
169         return -1;
170
171       if (elf->flags & ELF_F_LAYOUT)
172         {
173           /* The user is supposed to fill out e_phoff.  Use it and
174              e_phnum to determine the maximum extend.  */
175           size = MAX ((size_t) size,
176                       ehdr->e_phoff
177                       + elf_typesize (LIBELFBITS, ELF_T_PHDR, phnum));
178         }
179       else
180         {
181           update_if_changed (ehdr->e_phoff,
182                              elf_typesize (LIBELFBITS, ELF_T_EHDR, 1),
183                              ehdr_flags);
184
185           /* We need no alignment here.  */
186           size += elf_typesize (LIBELFBITS, ELF_T_PHDR, phnum);
187         }
188     }
189
190   if (shnum > 0)
191     {
192       Elf_ScnList *list;
193       bool first = true;
194
195       assert (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0);
196
197       if (shnum >= SHN_LORESERVE)
198         {
199           /* We have to  fill in the number of sections in the header
200              of the zeroth section.  */
201           Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
202
203           update_if_changed (scn0->shdr.ELFW(e,LIBELFBITS)->sh_size,
204                              shnum, scn0->shdr_flags);
205         }
206
207       /* Go over all sections and find out how large they are.  */
208       list = &elf->state.ELFW(elf,LIBELFBITS).scns;
209
210       /* Load the section headers if necessary.  This loads the
211          headers for all sections.  */
212       if (list->data[1].shdr.ELFW(e,LIBELFBITS) == NULL)
213         (void) __elfw2(LIBELFBITS,getshdr_wrlock) (&list->data[1]);
214
215       do
216         {
217           for (size_t cnt = first == true; cnt < list->cnt; ++cnt)
218             {
219               Elf_Scn *scn = &list->data[cnt];
220               ElfW2(LIBELFBITS,Shdr) *shdr = scn->shdr.ELFW(e,LIBELFBITS);
221               off_t offset = 0;
222
223               assert (shdr != NULL);
224               ElfW2(LIBELFBITS,Word) sh_entsize = shdr->sh_entsize;
225               ElfW2(LIBELFBITS,Word) sh_align = shdr->sh_addralign ?: 1;
226
227               /* Set the sh_entsize value if we can reliably detect it.  */
228               switch (shdr->sh_type)
229                 {
230                 case SHT_SYMTAB:
231                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYM, 1);
232                   break;
233                 case SHT_RELA:
234                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_RELA, 1);
235                   break;
236                 case SHT_GROUP:
237                   /* Only relocatable files can contain section groups.  */
238                   if (ehdr->e_type != ET_REL)
239                     {
240                       __libelf_seterrno (ELF_E_GROUP_NOT_REL);
241                       return -1;
242                     }
243                   /* FALLTHROUGH */
244                 case SHT_SYMTAB_SHNDX:
245                   sh_entsize = elf_typesize (32, ELF_T_WORD, 1);
246                   break;
247                 case SHT_HASH:
248                   sh_entsize = SH_ENTSIZE_HASH (ehdr);
249                   break;
250                 case SHT_DYNAMIC:
251                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_DYN, 1);
252                   break;
253                 case SHT_REL:
254                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_REL, 1);
255                   break;
256                 case SHT_DYNSYM:
257                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYM, 1);
258                   break;
259                 case SHT_SUNW_move:
260                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_MOVE, 1);
261                   break;
262                 case SHT_SUNW_syminfo:
263                   sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYMINFO, 1);
264                   break;
265                 default:
266                   break;
267                 }
268
269               /* If the section header contained the wrong entry size
270                  correct it and mark the header as modified.  */
271               update_if_changed (shdr->sh_entsize, sh_entsize,
272                                  scn->shdr_flags);
273
274               if (scn->data_read == 0
275                   && __libelf_set_rawdata_wrlock (scn) != 0)
276                 /* Something went wrong.  The error value is already set.  */
277                 return -1;
278
279               /* Iterate over all data blocks.  */
280               if (list->data[cnt].data_list_rear != NULL)
281                 {
282                   Elf_Data_List *dl = &scn->data_list;
283
284                   while (dl != NULL)
285                     {
286                       Elf_Data *data = &dl->data.d;
287                       if (dl == &scn->data_list && data->d_buf == NULL
288                           && scn->rawdata.d.d_buf != NULL)
289                         data = &scn->rawdata.d;
290
291                       if (unlikely (data->d_version == EV_NONE)
292                           || unlikely (data->d_version >= EV_NUM))
293                         {
294                           __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
295                           return -1;
296                         }
297
298                       if (unlikely (! powerof2 (data->d_align)))
299                         {
300                           __libelf_seterrno (ELF_E_INVALID_ALIGN);
301                           return -1;
302                         }
303
304                       sh_align = MAX (sh_align, data->d_align);
305
306                       if (elf->flags & ELF_F_LAYOUT)
307                         {
308                           /* The user specified the offset and the size.
309                              All we have to do is check whether this block
310                              fits in the size specified for the section.  */
311                           if (unlikely ((GElf_Word) (data->d_off
312                                                      + data->d_size)
313                                         > shdr->sh_size))
314                             {
315                               __libelf_seterrno (ELF_E_SECTION_TOO_SMALL);
316                               return -1;
317                             }
318                         }
319                       else
320                         {
321                           /* Determine the padding.  */
322                           offset = ((offset + data->d_align - 1)
323                                     & ~(data->d_align - 1));
324
325                           update_if_changed (data->d_off, offset, changed);
326
327                           offset += data->d_size;
328                         }
329
330                       /* Next data block.  */
331                       dl = dl->next;
332                     }
333                 }
334               else
335                 /* Get the size of the section from the raw data.  If
336                    none is available the value is zero.  */
337                 offset += scn->rawdata.d.d_size;
338
339               if (elf->flags & ELF_F_LAYOUT)
340                 {
341                   size = MAX ((GElf_Word) size,
342                               shdr->sh_offset
343                               + (shdr->sh_type != SHT_NOBITS
344                                  ? shdr->sh_size : 0));
345
346                   /* The alignment must be a power of two.  This is a
347                      requirement from the ELF specification.  Additionally
348                      we test for the alignment of the section being large
349                      enough for the largest alignment required by a data
350                      block.  */
351                   if (unlikely (! powerof2 (shdr->sh_addralign))
352                       || unlikely (shdr->sh_addralign < sh_align))
353                     {
354                       __libelf_seterrno (ELF_E_INVALID_ALIGN);
355                       return -1;
356                     }
357                 }
358               else
359                 {
360                   /* How much alignment do we need for this section.  */
361                   update_if_changed (shdr->sh_addralign, sh_align,
362                                      scn->shdr_flags);
363
364                   size = (size + sh_align - 1) & ~(sh_align - 1);
365                   int offset_changed = 0;
366                   update_if_changed (shdr->sh_offset, (GElf_Word) size,
367                                      offset_changed);
368                   changed |= offset_changed;
369
370                   if (offset_changed && scn->data_list_rear == NULL)
371                     {
372                       /* The position of the section in the file
373                          changed.  Create the section data list.  */
374                       if (__elf_getdata_rdlock (scn, NULL) == NULL)
375                         return -1;
376                     }
377
378                   /* See whether the section size is correct.  */
379                   update_if_changed (shdr->sh_size, (GElf_Word) offset,
380                                      changed);
381
382                   if (shdr->sh_type != SHT_NOBITS)
383                     size += offset;
384
385                   scn->flags |= changed;
386                 }
387
388               /* Check that the section size is actually a multiple of
389                  the entry size.  */
390               if (shdr->sh_entsize != 0
391                   && unlikely (shdr->sh_size % shdr->sh_entsize != 0)
392                   && (elf->flags & ELF_F_PERMISSIVE) == 0)
393                 {
394                   __libelf_seterrno (ELF_E_INVALID_SHENTSIZE);
395                   return -1;
396                 }
397             }
398
399           assert (list->next == NULL || list->cnt == list->max);
400
401           first = false;
402         }
403       while ((list = list->next) != NULL);
404
405       /* Store section information.  */
406       if (elf->flags & ELF_F_LAYOUT)
407         {
408           /* The user is supposed to fill out e_shoff.  Use it and
409              e_shnum (or sh_size of the dummy, first section header)
410              to determine the maximum extend.  */
411           size = MAX ((GElf_Word) size,
412                       (ehdr->e_shoff
413                        + (elf_typesize (LIBELFBITS, ELF_T_SHDR, shnum))));
414         }
415       else
416         {
417           /* Align for section header table.
418
419              Yes, we use `sizeof' and not `__alignof__' since we do not
420              want to be surprised by architectures with less strict
421              alignment rules.  */
422 #define SHDR_ALIGN sizeof (ElfW2(LIBELFBITS,Off))
423           size = (size + SHDR_ALIGN - 1) & ~(SHDR_ALIGN - 1);
424
425           update_if_changed (ehdr->e_shoff, (GElf_Word) size, elf->flags);
426           update_if_changed (ehdr->e_shentsize,
427                              elf_typesize (LIBELFBITS, ELF_T_SHDR, 1),
428                              ehdr_flags);
429
430           /* Account for the section header size.  */
431           size += elf_typesize (LIBELFBITS, ELF_T_SHDR, shnum);
432         }
433     }
434
435   elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ehdr_flags;
436
437   return size;
438 }