2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
2006-12-18 11:55:42 +00:00
|
|
|
* All rights reserved.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2006-12-18 11:55:42 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2006-12-18 11:55:42 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2007-09-14 20:12:55 +00:00
|
|
|
* @(#)$Id: elfloader.c,v 1.7 2007/09/14 20:12:55 nvt-se Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
|
2006-12-18 12:11:15 +00:00
|
|
|
#include "loader/elfloader.h"
|
2006-12-18 11:55:42 +00:00
|
|
|
#include "loader/elfloader-arch.h"
|
|
|
|
|
|
|
|
#include "cfs/cfs.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "loader/symtab.h"
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#if 0
|
2006-12-18 11:55:42 +00:00
|
|
|
#include <stdio.h>
|
2006-06-17 22:41:10 +00:00
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define EI_NIDENT 16
|
|
|
|
|
|
|
|
|
|
|
|
struct elf32_ehdr {
|
|
|
|
unsigned char e_ident[EI_NIDENT]; /* ident bytes */
|
2006-12-18 11:55:42 +00:00
|
|
|
elf32_half e_type; /* file type */
|
2006-06-17 22:41:10 +00:00
|
|
|
elf32_half e_machine; /* target machine */
|
|
|
|
elf32_word e_version; /* file version */
|
|
|
|
elf32_addr e_entry; /* start address */
|
|
|
|
elf32_off e_phoff; /* phdr file offset */
|
|
|
|
elf32_off e_shoff; /* shdr file offset */
|
|
|
|
elf32_word e_flags; /* file flags */
|
|
|
|
elf32_half e_ehsize; /* sizeof ehdr */
|
|
|
|
elf32_half e_phentsize; /* sizeof phdr */
|
|
|
|
elf32_half e_phnum; /* number phdrs */
|
|
|
|
elf32_half e_shentsize; /* sizeof shdr */
|
|
|
|
elf32_half e_shnum; /* number shdrs */
|
|
|
|
elf32_half e_shstrndx; /* shdr string index */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Values for e_type. */
|
|
|
|
#define ET_NONE 0 /* Unknown type. */
|
|
|
|
#define ET_REL 1 /* Relocatable. */
|
|
|
|
#define ET_EXEC 2 /* Executable. */
|
|
|
|
#define ET_DYN 3 /* Shared object. */
|
|
|
|
#define ET_CORE 4 /* Core file. */
|
|
|
|
|
|
|
|
struct elf32_shdr {
|
|
|
|
elf32_word sh_name; /* section name */
|
|
|
|
elf32_word sh_type; /* SHT_... */
|
|
|
|
elf32_word sh_flags; /* SHF_... */
|
|
|
|
elf32_addr sh_addr; /* virtual address */
|
|
|
|
elf32_off sh_offset; /* file offset */
|
|
|
|
elf32_word sh_size; /* section size */
|
|
|
|
elf32_word sh_link; /* misc info */
|
|
|
|
elf32_word sh_info; /* misc info */
|
|
|
|
elf32_word sh_addralign; /* memory alignment */
|
|
|
|
elf32_word sh_entsize; /* entry size if table */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* sh_type */
|
|
|
|
#define SHT_NULL 0 /* inactive */
|
|
|
|
#define SHT_PROGBITS 1 /* program defined information */
|
|
|
|
#define SHT_SYMTAB 2 /* symbol table section */
|
|
|
|
#define SHT_STRTAB 3 /* string table section */
|
|
|
|
#define SHT_RELA 4 /* relocation section with addends*/
|
|
|
|
#define SHT_HASH 5 /* symbol hash table section */
|
|
|
|
#define SHT_DYNAMIC 6 /* dynamic section */
|
|
|
|
#define SHT_NOTE 7 /* note section */
|
|
|
|
#define SHT_NOBITS 8 /* no space section */
|
|
|
|
#define SHT_REL 9 /* relation section without addends */
|
|
|
|
#define SHT_SHLIB 10 /* reserved - purpose unknown */
|
|
|
|
#define SHT_DYNSYM 11 /* dynamic symbol table section */
|
|
|
|
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
|
|
|
|
#define SHT_HIPROC 0x7fffffff /* specific section header types */
|
|
|
|
#define SHT_LOUSER 0x80000000 /* reserved range for application */
|
|
|
|
#define SHT_HIUSER 0xffffffff /* specific indexes */
|
|
|
|
|
|
|
|
struct elf32_rel {
|
|
|
|
elf32_addr r_offset; /* Location to be relocated. */
|
|
|
|
elf32_word r_info; /* Relocation type and symbol index. */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct elf32_sym {
|
|
|
|
elf32_word st_name; /* String table index of name. */
|
|
|
|
elf32_addr st_value; /* Symbol value. */
|
|
|
|
elf32_word st_size; /* Size of associated object. */
|
|
|
|
unsigned char st_info; /* Type and binding information. */
|
|
|
|
unsigned char st_other; /* Reserved (not used). */
|
|
|
|
elf32_half st_shndx; /* Section index of symbol. */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ELF32_R_SYM(info) ((info) >> 8)
|
|
|
|
#define ELF32_R_TYPE(info) ((unsigned char)(info))
|
|
|
|
|
|
|
|
struct relevant_section {
|
|
|
|
unsigned char number;
|
2006-12-18 11:55:42 +00:00
|
|
|
unsigned int offset;
|
2006-06-17 22:41:10 +00:00
|
|
|
char *address;
|
|
|
|
};
|
|
|
|
|
|
|
|
char elfloader_unknown[30]; /* Name that caused link error. */
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
struct process **elfloader_autostart_processes;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
static struct relevant_section bss, data, rodata, text;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-09-14 20:12:55 +00:00
|
|
|
static const unsigned char elf_magic_header[] =
|
2006-06-17 22:41:10 +00:00
|
|
|
{0x7f, 0x45, 0x4c, 0x46, /* 0x7f, 'E', 'L', 'F' */
|
|
|
|
0x01, /* Only 32-bit objects. */
|
|
|
|
0x01, /* Only LSB data. */
|
|
|
|
0x01, /* Only ELF version 1. */
|
|
|
|
};
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
seek_read(int fd, unsigned int offset, char *buf, int len)
|
|
|
|
{
|
|
|
|
cfs_seek(fd, offset);
|
|
|
|
cfs_read(fd, buf, len);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
static void
|
|
|
|
seek_write(int fd, unsigned int offset, char *buf, int len)
|
|
|
|
{
|
|
|
|
cfs_seek(fd, offset);
|
|
|
|
cfs_write(fd, buf, len);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void *
|
|
|
|
find_local_symbol(int fd, const char *symbol,
|
|
|
|
unsigned int symtab, unsigned short symtabsize,
|
|
|
|
unsigned int strtab)
|
|
|
|
{
|
|
|
|
struct elf32_sym s;
|
|
|
|
unsigned int a;
|
|
|
|
char name[30];
|
|
|
|
struct relevant_section *sect;
|
|
|
|
|
|
|
|
for(a = symtab; a < symtab + symtabsize; a += sizeof(s)) {
|
|
|
|
seek_read(fd, a, (char *)&s, sizeof(s));
|
|
|
|
|
|
|
|
if(s.st_name != 0) {
|
|
|
|
seek_read(fd, strtab + s.st_name, name, sizeof(name));
|
|
|
|
if(strcmp(name, symbol) == 0) {
|
|
|
|
if(s.st_shndx == bss.number) {
|
|
|
|
sect = &bss;
|
|
|
|
} else if(s.st_shndx == data.number) {
|
|
|
|
sect = &data;
|
|
|
|
} else if(s.st_shndx == text.number) {
|
|
|
|
sect = &text;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &(sect->address[s.st_value]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2006-12-18 11:55:42 +00:00
|
|
|
relocate_section(int fd,
|
|
|
|
unsigned int section, unsigned short size,
|
|
|
|
unsigned int sectionaddr,
|
2006-12-18 15:35:16 +00:00
|
|
|
char *sectionbase,
|
2006-12-18 11:55:42 +00:00
|
|
|
unsigned int strs,
|
|
|
|
unsigned int strtab,
|
2006-12-18 15:35:16 +00:00
|
|
|
unsigned int symtab, unsigned short symtabsize,
|
|
|
|
unsigned char using_relas)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
2006-12-18 15:35:16 +00:00
|
|
|
/* sectionbase added; runtime start address of current section */
|
|
|
|
struct elf32_rela rela; /* Now used both for rel and rela data! */
|
|
|
|
int rel_size = 0;
|
2006-06-17 22:41:10 +00:00
|
|
|
struct elf32_sym s;
|
2006-12-18 11:55:42 +00:00
|
|
|
unsigned int a;
|
2006-06-17 22:41:10 +00:00
|
|
|
char name[30];
|
|
|
|
char *addr;
|
|
|
|
struct relevant_section *sect;
|
2006-12-18 15:35:16 +00:00
|
|
|
|
|
|
|
/* determine correct relocation entry sizes */
|
|
|
|
if(using_relas) {
|
|
|
|
rel_size = sizeof(struct elf32_rela);
|
|
|
|
} else {
|
|
|
|
rel_size = sizeof(struct elf32_rel);
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
for(a = section; a < section + size; a += rel_size) {
|
|
|
|
seek_read(fd, a, (char *)&rela, rel_size);
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd,
|
|
|
|
symtab + sizeof(struct elf32_sym) * ELF32_R_SYM(rela.r_info),
|
|
|
|
(char *)&s, sizeof(s));
|
|
|
|
if(s.st_name != 0) {
|
|
|
|
seek_read(fd, strtab + s.st_name, name, sizeof(name));
|
|
|
|
PRINTF("name: %s\n", name);
|
2006-06-17 22:41:10 +00:00
|
|
|
addr = (char *)symtab_lookup(name);
|
2006-12-18 11:55:42 +00:00
|
|
|
/* ADDED */
|
|
|
|
if(addr == NULL) {
|
|
|
|
PRINTF("name not found in global: %s\n", name);
|
|
|
|
addr = find_local_symbol(fd, name, symtab, symtabsize, strtab);
|
|
|
|
PRINTF("found address %p\n", addr);
|
|
|
|
}
|
|
|
|
if(addr == NULL) {
|
|
|
|
if(s.st_shndx == bss.number) {
|
2006-06-17 22:41:10 +00:00
|
|
|
sect = &bss;
|
|
|
|
} else if(s.st_shndx == data.number) {
|
|
|
|
sect = &data;
|
2006-12-18 15:35:16 +00:00
|
|
|
} else if(s.st_shndx == rodata.number) {
|
|
|
|
sect = &rodata;
|
2006-06-17 22:41:10 +00:00
|
|
|
} else if(s.st_shndx == text.number) {
|
|
|
|
sect = &text;
|
|
|
|
} else {
|
2006-12-18 11:55:42 +00:00
|
|
|
PRINTF("elfloader unknown name: '%30s'\n", name);
|
2006-06-17 22:41:10 +00:00
|
|
|
memcpy(elfloader_unknown, name, sizeof(elfloader_unknown));
|
|
|
|
elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0;
|
|
|
|
return ELFLOADER_SYMBOL_NOT_FOUND;
|
|
|
|
}
|
2006-12-18 11:55:42 +00:00
|
|
|
addr = sect->address;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(s.st_shndx == bss.number) {
|
|
|
|
sect = &bss;
|
|
|
|
} else if(s.st_shndx == data.number) {
|
|
|
|
sect = &data;
|
2006-12-18 15:35:16 +00:00
|
|
|
} else if(s.st_shndx == rodata.number) {
|
|
|
|
sect = &rodata;
|
2006-12-18 11:55:42 +00:00
|
|
|
} else if(s.st_shndx == text.number) {
|
|
|
|
sect = &text;
|
|
|
|
} else {
|
|
|
|
return ELFLOADER_SEGMENT_NOT_FOUND;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2006-12-18 11:55:42 +00:00
|
|
|
|
|
|
|
addr = sect->address;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2006-12-18 11:55:42 +00:00
|
|
|
|
2007-03-22 23:56:44 +00:00
|
|
|
if(!using_relas) {
|
|
|
|
/* copy addend to rela structure */
|
|
|
|
seek_read(fd, sectionaddr + rela.r_offset, (char *)&rela.r_addend, 4);
|
|
|
|
}
|
2006-12-18 15:35:16 +00:00
|
|
|
|
|
|
|
elfloader_arch_relocate(fd, sectionaddr, sectionbase, &rela, addr);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
return ELFLOADER_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-18 11:55:42 +00:00
|
|
|
static void *
|
|
|
|
find_program_processes(int fd,
|
|
|
|
unsigned int symtab, unsigned short size,
|
|
|
|
unsigned int strtab)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
struct elf32_sym s;
|
2006-12-18 11:55:42 +00:00
|
|
|
unsigned int a;
|
2006-06-17 22:41:10 +00:00
|
|
|
char name[30];
|
2006-12-18 11:55:42 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
for(a = symtab; a < symtab + size; a += sizeof(s)) {
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, a, (char *)&s, sizeof(s));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
if(s.st_name != 0) {
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, strtab + s.st_name, name, sizeof(name));
|
|
|
|
if(strcmp(name, "autostart_processes") == 0) {
|
|
|
|
return &data.address[s.st_value];
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-18 11:55:42 +00:00
|
|
|
return NULL;
|
|
|
|
/* return find_local_symbol(fd, "autostart_processes", symtab, size, strtab); */
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-18 11:55:42 +00:00
|
|
|
void
|
|
|
|
elfloader_init(void)
|
|
|
|
{
|
|
|
|
elfloader_autostart_processes = NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if 0
|
|
|
|
static void
|
|
|
|
print_chars(unsigned char *ptr, int num)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < num; ++i) {
|
|
|
|
PRINTF("%d", ptr[i]);
|
|
|
|
if(i == num - 1) {
|
|
|
|
PRINTF("\n");
|
|
|
|
} else {
|
|
|
|
PRINTF(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* 0 */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-17 22:41:10 +00:00
|
|
|
int
|
2006-12-18 11:55:42 +00:00
|
|
|
elfloader_load(int fd)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
struct elf32_ehdr ehdr;
|
|
|
|
struct elf32_shdr shdr;
|
|
|
|
struct elf32_shdr strtable;
|
2006-12-18 11:55:42 +00:00
|
|
|
unsigned int strs;
|
|
|
|
unsigned int shdrptr;
|
|
|
|
unsigned int nameptr;
|
2006-06-17 22:41:10 +00:00
|
|
|
char name[12];
|
|
|
|
|
|
|
|
int i;
|
|
|
|
unsigned short shdrnum, shdrsize;
|
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
unsigned char using_relas = -1;
|
2006-06-17 22:41:10 +00:00
|
|
|
unsigned short textoff = 0, textsize, textrelaoff = 0, textrelasize;
|
|
|
|
unsigned short dataoff = 0, datasize, datarelaoff = 0, datarelasize;
|
2006-12-18 15:35:16 +00:00
|
|
|
unsigned short rodataoff = 0, rodatasize, rodatarelaoff = 0, rodatarelasize;
|
2006-06-17 22:41:10 +00:00
|
|
|
unsigned short symtaboff = 0, symtabsize;
|
|
|
|
unsigned short strtaboff = 0, strtabsize;
|
|
|
|
unsigned short bsssize = 0;
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
struct process **process;
|
2006-06-17 22:41:10 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
elfloader_unknown[0] = 0;
|
|
|
|
|
|
|
|
/* The ELF header is located at the start of the buffer. */
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, 0, (char *)&ehdr, sizeof(ehdr));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
/* print_chars(ehdr.e_ident, sizeof(elf_magic_header));
|
|
|
|
print_chars(elf_magic_header, sizeof(elf_magic_header));*/
|
2006-06-17 22:41:10 +00:00
|
|
|
/* Make sure that we have a correct and compatible ELF header. */
|
|
|
|
if(memcmp(ehdr.e_ident, elf_magic_header, sizeof(elf_magic_header)) != 0) {
|
2006-12-18 11:55:42 +00:00
|
|
|
PRINTF("ELF header problems\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
return ELFLOADER_BAD_ELF_HEADER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab the section header. */
|
2006-12-18 11:55:42 +00:00
|
|
|
shdrptr = ehdr.e_shoff;
|
|
|
|
seek_read(fd, shdrptr, (char *)&shdr, sizeof(shdr));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/* Get the size and number of entries of the section header. */
|
|
|
|
shdrsize = ehdr.e_shentsize;
|
|
|
|
shdrnum = ehdr.e_shnum;
|
|
|
|
|
|
|
|
/* The string table section: holds the names of the sections. */
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, ehdr.e_shoff + shdrsize * ehdr.e_shstrndx,
|
|
|
|
(char *)&strtable, sizeof(strtable));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/* Get a pointer to the actual table of strings. This table holds
|
|
|
|
the names of the sections, not the names of other symbols in the
|
|
|
|
file (these are in the sybtam section). */
|
2006-12-18 11:55:42 +00:00
|
|
|
strs = strtable.sh_offset;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/* Go through all sections and pick out the relevant ones. The
|
|
|
|
".text" segment holds the actual code from the ELF file, the
|
|
|
|
".data" segment contains initialized data, the ".bss" segment
|
2006-12-18 15:35:16 +00:00
|
|
|
holds the size of the unitialized data segment. The ".rel[a].text"
|
|
|
|
and ".rel[a].data" segments contains relocation information for the
|
2006-06-17 22:41:10 +00:00
|
|
|
contents of the ".text" and ".data" segments, respectively. The
|
|
|
|
".symtab" segment contains the symbol table for this file. The
|
|
|
|
".strtab" segment points to the actual string names used by the
|
|
|
|
symbol table.
|
|
|
|
|
|
|
|
In addition to grabbing pointers to the relevant sections, we
|
|
|
|
also save the section number for resolving addresses in the
|
|
|
|
relocator code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize the segment sizes to zero so that we can check if
|
|
|
|
their sections was found in the file or not. */
|
|
|
|
textsize = textrelasize = datasize = datarelasize =
|
2006-12-18 15:35:16 +00:00
|
|
|
rodatasize = rodatarelasize = symtabsize = strtabsize = 0;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
bss.number = data.number = rodata.number = text.number = -1;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
shdrptr = ehdr.e_shoff;
|
2006-06-17 22:41:10 +00:00
|
|
|
for(i = 0; i < shdrnum; ++i) {
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, shdrptr, (char *)&shdr, sizeof(shdr));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/* The name of the section is contained in the strings table. */
|
|
|
|
nameptr = strs + shdr.sh_name;
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, nameptr, name, sizeof(name));
|
2007-03-22 23:56:44 +00:00
|
|
|
PRINTF("Section shdrptr 0x%x, %d + %d type %d\n",
|
|
|
|
shdrptr,
|
|
|
|
strs, shdr.sh_name,
|
|
|
|
(int)shdr.sh_type);
|
2006-06-17 22:41:10 +00:00
|
|
|
/* Match the name of the section with a predefined set of names
|
|
|
|
(.text, .data, .bss, .rela.text, .rela.data, .symtab, and
|
|
|
|
.strtab). */
|
2006-12-18 15:35:16 +00:00
|
|
|
/* added support for .rodata, .rel.text and .rel.data). */
|
2006-12-18 11:55:42 +00:00
|
|
|
|
2007-03-22 23:56:44 +00:00
|
|
|
if(shdr.sh_type == SHT_SYMTAB/*strncmp(name, ".symtab", 7) == 0*/) {
|
|
|
|
PRINTF("symtab\n");
|
|
|
|
symtaboff = shdr.sh_offset;
|
|
|
|
symtabsize = shdr.sh_size;
|
|
|
|
} else if(shdr.sh_type == SHT_STRTAB/*strncmp(name, ".strtab", 7) == 0*/) {
|
|
|
|
PRINTF("strtab\n");
|
|
|
|
strtaboff = shdr.sh_offset;
|
|
|
|
strtabsize = shdr.sh_size;
|
|
|
|
} else if(strncmp(name, ".text", 5) == 0) {
|
2006-06-17 22:41:10 +00:00
|
|
|
textoff = shdr.sh_offset;
|
|
|
|
textsize = shdr.sh_size;
|
|
|
|
text.number = i;
|
2006-12-18 11:55:42 +00:00
|
|
|
text.offset = textoff;
|
2006-12-18 15:35:16 +00:00
|
|
|
} else if(strncmp(name, ".rel.text", 9) == 0) {
|
|
|
|
using_relas = 0;
|
|
|
|
textrelaoff = shdr.sh_offset;
|
|
|
|
textrelasize = shdr.sh_size;
|
2006-06-17 22:41:10 +00:00
|
|
|
} else if(strncmp(name, ".rela.text", 10) == 0) {
|
2006-12-18 15:35:16 +00:00
|
|
|
using_relas = 1;
|
2006-06-17 22:41:10 +00:00
|
|
|
textrelaoff = shdr.sh_offset;
|
|
|
|
textrelasize = shdr.sh_size;
|
|
|
|
} else if(strncmp(name, ".data", 5) == 0) {
|
|
|
|
dataoff = shdr.sh_offset;
|
|
|
|
datasize = shdr.sh_size;
|
|
|
|
data.number = i;
|
2006-12-18 11:55:42 +00:00
|
|
|
data.offset = dataoff;
|
2006-12-18 15:35:16 +00:00
|
|
|
} else if(strncmp(name, ".rodata", 7) == 0) {
|
|
|
|
/* read-only data handled the same way as regular text section */
|
|
|
|
rodataoff = shdr.sh_offset;
|
|
|
|
rodatasize = shdr.sh_size;
|
|
|
|
rodata.number = i;
|
|
|
|
rodata.offset = rodataoff;
|
|
|
|
} else if(strncmp(name, ".rel.rodata", 11) == 0) {
|
|
|
|
/* using elf32_rel instead of rela */
|
|
|
|
using_relas = 0;
|
|
|
|
rodatarelaoff = shdr.sh_offset;
|
|
|
|
rodatarelasize = shdr.sh_size;
|
|
|
|
} else if(strncmp(name, ".rela.rodata", 12) == 0) {
|
|
|
|
using_relas = 1;
|
|
|
|
rodatarelaoff = shdr.sh_offset;
|
|
|
|
rodatarelasize = shdr.sh_size;
|
|
|
|
} else if(strncmp(name, ".rel.data", 9) == 0) {
|
|
|
|
/* using elf32_rel instead of rela */
|
|
|
|
using_relas = 0;
|
|
|
|
datarelaoff = shdr.sh_offset;
|
|
|
|
datarelasize = shdr.sh_size;
|
2006-06-17 22:41:10 +00:00
|
|
|
} else if(strncmp(name, ".rela.data", 10) == 0) {
|
2006-12-18 15:35:16 +00:00
|
|
|
using_relas = 1;
|
2006-06-17 22:41:10 +00:00
|
|
|
datarelaoff = shdr.sh_offset;
|
|
|
|
datarelasize = shdr.sh_size;
|
|
|
|
} else if(strncmp(name, ".bss", 4) == 0) {
|
|
|
|
bsssize = shdr.sh_size;
|
|
|
|
bss.number = i;
|
2006-12-18 11:55:42 +00:00
|
|
|
bss.offset = 0;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Move on to the next section header. */
|
|
|
|
shdrptr += shdrsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(symtabsize == 0) {
|
|
|
|
return ELFLOADER_NO_SYMTAB;
|
|
|
|
}
|
|
|
|
if(strtabsize == 0) {
|
|
|
|
return ELFLOADER_NO_STRTAB;
|
|
|
|
}
|
|
|
|
if(textsize == 0) {
|
|
|
|
return ELFLOADER_NO_TEXT;
|
|
|
|
}
|
|
|
|
|
2007-03-22 23:56:44 +00:00
|
|
|
PRINTF("before allocate ram\n");
|
2006-12-18 11:55:42 +00:00
|
|
|
bss.address = (char *)elfloader_arch_allocate_ram(bsssize + datasize);
|
|
|
|
data.address = (char *)bss.address + bsssize;
|
2007-03-22 23:56:44 +00:00
|
|
|
PRINTF("before allocate rom\n");
|
2006-12-18 15:35:16 +00:00
|
|
|
text.address = (char *)elfloader_arch_allocate_rom(textsize + rodatasize);
|
|
|
|
rodata.address = (char *)text.address + textsize;
|
2006-12-18 11:55:42 +00:00
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
|
2007-03-22 23:56:44 +00:00
|
|
|
PRINTF("bss base address: bss.address = 0x%08x\n", bss.address);
|
|
|
|
PRINTF("data base address: data.address = 0x%08x\n", data.address);
|
|
|
|
PRINTF("text base address: text.address = 0x%08x\n", text.address);
|
|
|
|
PRINTF("rodata base address: rodata.address = 0x%08x\n", rodata.address);
|
2006-12-18 15:35:16 +00:00
|
|
|
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
/* If we have text segment relocations, we process them. */
|
|
|
|
PRINTF("elfloader: relocate text\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
if(textrelasize > 0) {
|
2006-12-18 15:35:16 +00:00
|
|
|
ret = relocate_section(fd,
|
2006-12-18 11:55:42 +00:00
|
|
|
textrelaoff, textrelasize,
|
|
|
|
textoff,
|
2006-12-18 15:35:16 +00:00
|
|
|
text.address,
|
2006-06-17 22:41:10 +00:00
|
|
|
strs,
|
2006-12-18 11:55:42 +00:00
|
|
|
strtaboff,
|
2006-12-18 15:35:16 +00:00
|
|
|
symtaboff, symtabsize, using_relas);
|
2006-06-17 22:41:10 +00:00
|
|
|
if(ret != ELFLOADER_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2006-12-18 11:55:42 +00:00
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
/* If we have any rodata segment relocations, we process them too. */
|
|
|
|
PRINTF("elfloader: relocate rodata\n");
|
|
|
|
if(rodatarelasize > 0) {
|
|
|
|
ret = relocate_section(fd,
|
|
|
|
rodatarelaoff, rodatarelasize,
|
|
|
|
rodataoff,
|
|
|
|
rodata.address,
|
|
|
|
strs,
|
|
|
|
strtaboff,
|
|
|
|
symtaboff, symtabsize, using_relas);
|
|
|
|
if(ret != ELFLOADER_OK) {
|
|
|
|
PRINTF("elfloader: data failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
/* If we have any data segment relocations, we process them too. */
|
|
|
|
PRINTF("elfloader: relocate data\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
if(datarelasize > 0) {
|
2006-12-18 11:55:42 +00:00
|
|
|
ret = relocate_section(fd,
|
|
|
|
datarelaoff, datarelasize,
|
|
|
|
dataoff,
|
2006-12-18 15:35:16 +00:00
|
|
|
data.address,
|
2006-06-17 22:41:10 +00:00
|
|
|
strs,
|
2006-12-18 11:55:42 +00:00
|
|
|
strtaboff,
|
2006-12-18 15:35:16 +00:00
|
|
|
symtaboff, symtabsize, using_relas);
|
2006-06-17 22:41:10 +00:00
|
|
|
if(ret != ELFLOADER_OK) {
|
2006-12-18 11:55:42 +00:00
|
|
|
PRINTF("elfloader: data failed\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-18 15:35:16 +00:00
|
|
|
/* Write text and rodata segment into flash and data segment into RAM. */
|
|
|
|
elfloader_arch_write_rom(fd, textoff, textsize, text.address);
|
|
|
|
elfloader_arch_write_rom(fd, rodataoff, rodatasize, rodata.address);
|
2006-12-18 11:55:42 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
memset(bss.address, 0, bsssize);
|
2006-12-18 11:55:42 +00:00
|
|
|
seek_read(fd, dataoff, data.address, datasize);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-18 11:55:42 +00:00
|
|
|
PRINTF("elfloader: autostart search\n");
|
Support for z80(sdcc) port.
In order to support, some core modules are modified as follows:
core/sys/dsc.h
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
- DSC_HEADER is changed to remove extra semicolon.
core/sys/process.h
- process_data_t is expressed by void* in signatures (known bug on sdcc).
core/sys/autostart.h
- autostart_processes is changed to remove extra semicolon.
core/sys/cc.h
- CC_CONF_ASSIGN_AGGREGATE is introduced.
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/net/hc.c
core/net/uip_arp.c
core/net/uaodv.c
- Aggregation assignments are changed to uip_ipaddr_copy.
core/net/psock.c
core/net/uipbuf.c
core/net/dhcpc.c
apps/shell/shell.c
core/ctk/vnc-server.c
core/ctk/vnc-out.c
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
core/net/uip-over-mesh.c
- An extra semicolon is removed.
apps/dhcp/dhcp-dsc.c
apps/shell/shell-dsc.
apps/ftp/ftp-dsc.c
apps/process-list/process-list-dsc.c
apps/email/email-dsc.c
apps/webserver/webserver-dsc.c
apps/vnc/vnc-dsc.c
apps/vnc/vnc-viewer.h
apps/webbrowser/www-dsc.c
apps/about/about-dsc.c
apps/irc/irc-dsc.c
apps/telnet/telnet-dsc.c
apps/telnetd/telnetd-dsc.c
apps/netconf/netconf-dsc.c
apps/directory/directory-dsc.c
pps/calc/calc-dsc.c
- Modify an extern type to a real declaration, which is static
to prevent a compile error.
core/net/mac/xmac.c
- Variables cannot be defined in a head of block on sdcc.
core/ctk/ctk.h
core/ctk/ctk.c
apps/program-handler/program-handler.c
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
Makefile.include
- Add a set of configuration for an assembler.
- $(CLEAN) variable is introduced for customized cleanup.
apps/process-list/process-list.c
- PROCESSLIST_CONF_HEIGHT is introduced to address smaller screen size.
core/lib/ctk-filedialog.c
- FILES_CONF_HEIGHT is introduced to address smaller screen size.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/vnc/vnc-viewer.c
- A cast is added to prevent a compile error.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/webbrowser/webclient.c
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/loader/elfloader.c
- A cast is added to prevent a compile error.
core/net/rime/rimeaddr.c
- An initialization is added to prevent a compile error.
core/net/rime/rudolph0.c
- NULL is changed to 0, because NULL causes a compile error.
core/net/rime/route-discovery.c
- Add an argument to match the definition of nf_callbacks.
cpu/z80/strcasecmp.h
cpu/z80/strcasecmp.c
cpu/z80/contiki-sdcc-conf.h
cpu/z80/mtarch.c
cpu/z80/mtarch.h
cpu/z80/Makefile.z80
- New files to make compilation availble on sdcc.
- Added support for multithreading.
2007-08-30 14:39:16 +00:00
|
|
|
process = (struct process **) find_local_symbol(fd, "autostart_processes", symtaboff, symtabsize, strtaboff);
|
2006-12-18 11:55:42 +00:00
|
|
|
if(process != NULL) {
|
|
|
|
PRINTF("elfloader: autostart found\n");
|
|
|
|
elfloader_autostart_processes = process;
|
2006-06-17 22:41:10 +00:00
|
|
|
return ELFLOADER_OK;
|
|
|
|
} else {
|
2006-12-18 11:55:42 +00:00
|
|
|
PRINTF("elfloader: no autostart\n");
|
Support for z80(sdcc) port.
In order to support, some core modules are modified as follows:
core/sys/dsc.h
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
- DSC_HEADER is changed to remove extra semicolon.
core/sys/process.h
- process_data_t is expressed by void* in signatures (known bug on sdcc).
core/sys/autostart.h
- autostart_processes is changed to remove extra semicolon.
core/sys/cc.h
- CC_CONF_ASSIGN_AGGREGATE is introduced.
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/net/hc.c
core/net/uip_arp.c
core/net/uaodv.c
- Aggregation assignments are changed to uip_ipaddr_copy.
core/net/psock.c
core/net/uipbuf.c
core/net/dhcpc.c
apps/shell/shell.c
core/ctk/vnc-server.c
core/ctk/vnc-out.c
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
core/net/uip-over-mesh.c
- An extra semicolon is removed.
apps/dhcp/dhcp-dsc.c
apps/shell/shell-dsc.
apps/ftp/ftp-dsc.c
apps/process-list/process-list-dsc.c
apps/email/email-dsc.c
apps/webserver/webserver-dsc.c
apps/vnc/vnc-dsc.c
apps/vnc/vnc-viewer.h
apps/webbrowser/www-dsc.c
apps/about/about-dsc.c
apps/irc/irc-dsc.c
apps/telnet/telnet-dsc.c
apps/telnetd/telnetd-dsc.c
apps/netconf/netconf-dsc.c
apps/directory/directory-dsc.c
pps/calc/calc-dsc.c
- Modify an extern type to a real declaration, which is static
to prevent a compile error.
core/net/mac/xmac.c
- Variables cannot be defined in a head of block on sdcc.
core/ctk/ctk.h
core/ctk/ctk.c
apps/program-handler/program-handler.c
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
Makefile.include
- Add a set of configuration for an assembler.
- $(CLEAN) variable is introduced for customized cleanup.
apps/process-list/process-list.c
- PROCESSLIST_CONF_HEIGHT is introduced to address smaller screen size.
core/lib/ctk-filedialog.c
- FILES_CONF_HEIGHT is introduced to address smaller screen size.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/vnc/vnc-viewer.c
- A cast is added to prevent a compile error.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/webbrowser/webclient.c
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/loader/elfloader.c
- A cast is added to prevent a compile error.
core/net/rime/rimeaddr.c
- An initialization is added to prevent a compile error.
core/net/rime/rudolph0.c
- NULL is changed to 0, because NULL causes a compile error.
core/net/rime/route-discovery.c
- Add an argument to match the definition of nf_callbacks.
cpu/z80/strcasecmp.h
cpu/z80/strcasecmp.c
cpu/z80/contiki-sdcc-conf.h
cpu/z80/mtarch.c
cpu/z80/mtarch.h
cpu/z80/Makefile.z80
- New files to make compilation availble on sdcc.
- Added support for multithreading.
2007-08-30 14:39:16 +00:00
|
|
|
process = (struct process **) find_program_processes(fd, symtaboff, symtabsize, strtaboff);
|
2006-12-18 11:55:42 +00:00
|
|
|
if(process != NULL) {
|
|
|
|
PRINTF("elfloader: FOUND PRG\n");
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
return ELFLOADER_NO_STARTPOINT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|