summaryrefslogtreecommitdiff
path: root/libc-parts/loader.c
blob: d0f26b8536f9903f3bee88178eeada48e971af39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* loader.c - Load ELF files.
   Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc.
   Written by Marcus Brinkmann and Neal H. Walfield.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <hurd/stddef.h>
#include <string.h>
#include <bits/wordsize.h>
#include <endian.h>

#include "loader.h"
#include "elf.h"

#define MIN(a, b) ((a) < (b) ? (a) : (b))

bool
loader_elf_load (loader_allocate_object_callback_t alloc,
		 loader_lookup_object_callback_t lookup,
		 void *start, void *end, uintptr_t *entry)
{
  Elf32_Ehdr *elf = (Elf32_Ehdr *) start;

  if (elf->e_ident[EI_MAG0] != ELFMAG0
      || elf->e_ident[EI_MAG1] != ELFMAG1
      || elf->e_ident[EI_MAG2] != ELFMAG2
      || elf->e_ident[EI_MAG3] != ELFMAG3)
    {
      debug (0, "Not an ELF file");
      return false;
    }

  if (elf->e_type != ET_EXEC)
    {
      debug (0, "Not an executable file");
      return false;
    }

  if (!elf->e_phoff)
    {
      debug (0, "No valid program header offset");
      return false;
    }

  /* FIXME: Some architectures support both word sizes.  */
  if (!((elf->e_ident[EI_CLASS] == ELFCLASS32
	 && __WORDSIZE == 32)
	|| (elf->e_ident[EI_CLASS] == ELFCLASS64
	    && __WORDSIZE == 64)))
    {
      debug (0, "Invalid word size");
      return false;
    }

  if (!((elf->e_ident[EI_DATA] == ELFDATA2LSB
	 && __BYTE_ORDER == __LITTLE_ENDIAN)
	|| (elf->e_ident[EI_DATA] == ELFDATA2MSB
	    && __BYTE_ORDER == __BIG_ENDIAN)))
    {
      debug (0, "Invalid byte order");
      return false;
    }

#if i386
# define elf_machine EM_386
#elif PPC
# define elf_machine EM_PPC
#else
# error Not ported to this architecture!
#endif

  if (elf->e_machine != elf_machine)
    {
      debug (0, "Binary not for this architecture");
      return false;
    }

  /* We have an ELF file.  Load it.  */

  int i;
  for (i = 0; i < elf->e_phnum; i++)
    {
      Elf32_Phdr *ph = (Elf32_Phdr *) (start + elf->e_phoff
				       + i * elf->e_phentsize);
      if (ph->p_type != PT_LOAD)
	continue;

      /* Load this section.  */

      bool ro = ! (ph->p_flags & PF_W);

      uintptr_t addr = ph->p_paddr;

      /* Offset of PH->P_PADDR in the first page.  */
      int offset = ph->p_paddr & (PAGESIZE - 1);
      if (offset)
	/* This section does not start on a page aligned address.  It
	   may be the case that another section is on this page.  If
	   so, don't allocate a new page but use the existing one.  */
	{
	  void *page = lookup (addr - offset);
	  if (! page)
	    page = alloc (addr - offset, ro);

	  /* Copy the data that belongs on the first page.  */
	  memcpy ((void *) page + offset,
		  (void *) start + ph->p_offset,
		  MIN (PAGESIZE - offset, ph->p_filesz));

	  addr = addr - offset + PAGESIZE;
	}

      /* We know process the section a page at a time.  */
      assert ((addr & (PAGESIZE - 1)) == 0);
      for (; addr < ph->p_paddr + ph->p_memsz; addr += PAGESIZE)
	{
	  /* Allocate a page.  */
	  struct vg_object *page = NULL;

	  if (ph->p_paddr + ph->p_memsz < addr + PAGESIZE)
	    /* We have less than a page of data to process.  Another
	       section could have written data to the end of this
	       page.  See if such a page has already been
	       allocated.  */
	    page = lookup (addr);

	  if (! page)
	    page = alloc (addr, ro);

	  if (addr < ph->p_paddr + ph->p_filesz)
	    memcpy ((void *) page,
		    (void *) start + ph->p_offset + (addr - ph->p_paddr),
		    MIN (PAGESIZE, ph->p_paddr + ph->p_filesz - addr));
	}
    }

  if (entry)
    *entry = elf->e_entry;

  return true;
}