Initial revision

CVS patchset: 5808
CVS date: 2002/10/23 16:02:16
This commit is contained in:
jbj 2002-10-23 16:02:16 +00:00
parent ad513051bd
commit aecacb8eaf
3 changed files with 491 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/* Add signed little endian base 128 integer to a section.
Copyright (C) 2002 Red Hat, Inc.
Written by Ulrich Drepper <drepper@redhat.com>, 2002.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <inttypes.h>
#include <string.h>
#include <libasmP.h>
int
asm_addsleb128 (asmscn, num)
AsmScn_t *asmscn;
int32_t num;
{
if (asmscn == NULL)
return -1;
if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
{
__libasm_seterrno (ASM_E_TYPE);
return -1;
}
if (unlikely (asmscn->ctx->textp))
printf ("\t.sleb128\t%" PRId32 "\n", num);
else
{
char tmpbuf[(sizeof (num) * 8 + 6) / 7];
char *dest = tmpbuf;
uint32_t byte;
int32_t endval = num >> 31;
if (num == 0)
byte = 0;
else
while (1)
{
byte = num & 0x7f;
num >>= 7;
if (num == endval)
/* This is the last byte. */
break;
*dest++ = byte | 0x80;
}
*dest++ = byte;
/* Number of bytes produced. */
size_t nbytes = dest - tmpbuf;
/* Make sure we have enough room. */
if (__libasm_ensure_section_space (asmscn, nbytes) != 0)
return -1;
/* Copy the bytes. */
if (likely (asmscn->type != SHT_NOBITS))
memcpy (&asmscn->content->data[asmscn->content->len], tmpbuf, nbytes);
/* Adjust the pointer in the data buffer. */
asmscn->content->len += nbytes;
/* Increment the offset in the (sub)section. */
asmscn->offset += nbytes;
}
return 0;
}

View File

@ -0,0 +1,83 @@
/* Add integer to a section.
Copyright (C) 2002 Red Hat, Inc.
Written by Ulrich Drepper <drepper@redhat.com>, 2002.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <inttypes.h>
#include <string.h>
#include "libasmP.h"
int
asm_adduleb128 (asmscn, num)
AsmScn_t *asmscn;
uint32_t num;
{
if (asmscn == NULL)
return -1;
if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
{
__libasm_seterrno (ASM_E_TYPE);
return -1;
}
if (unlikely (asmscn->ctx->textp))
printf ("\t.uleb128\t%" PRIu32 "\n", num);
else
{
char tmpbuf[(sizeof (num) * 8 + 6) / 7];
char *dest = tmpbuf;
uint32_t byte;
while (1)
{
byte = num & 0x7f;
num >>= 7;
if (num == 0)
/* This is the last byte. */
break;
*dest++ = byte | 0x80;
}
*dest++ = byte;
/* Number of bytes produced. */
size_t nbytes = dest - tmpbuf;
/* Make sure we have enough room. */
if (__libasm_ensure_section_space (asmscn, nbytes) != 0)
return -1;
/* Copy the bytes. */
if (likely (asmscn->type != SHT_NOBITS))
memcpy (&asmscn->content->data[asmscn->content->len], tmpbuf, nbytes);
/* Adjust the pointer in the data buffer. */
asmscn->content->len += nbytes;
/* Increment the offset in the (sub)section. */
asmscn->offset += nbytes;
}
return 0;
}

321
elfutils/tests/asm-tst9.c Normal file
View File

@ -0,0 +1,321 @@
/* Copyright (C) 2002 Red Hat, Inc.
Written by Ulrich Drepper <drepper@redhat.com>, 2002.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <fcntl.h>
#include <inttypes.h>
#include <libasm.h>
#include <libelf.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static const char fname[] = "asm-tst9-out.o";
static int32_t input[] =
{
0, 1, 129, 510, 2000, 33000, 0x7ffffff, 0x7fffffff
};
#define ninput (sizeof (input) / sizeof (input[0]))
static const GElf_Ehdr expected_ehdr =
{
.e_ident = { [EI_MAG0] = ELFMAG0,
[EI_MAG1] = ELFMAG1,
[EI_MAG2] = ELFMAG2,
[EI_MAG3] = ELFMAG3,
[EI_CLASS] = ELFCLASS32,
[EI_DATA] = ELFDATA2LSB,
[EI_VERSION] = EV_CURRENT },
.e_type = ET_REL,
.e_machine = EM_386,
.e_version = EV_CURRENT,
.e_shoff = 180,
.e_ehsize = sizeof (Elf32_Ehdr),
.e_shentsize = sizeof (Elf32_Shdr),
.e_shnum = 3,
.e_shstrndx = 2
};
static const char *scnnames[3] =
{
[0] = "",
[1] = ".data",
[2] = ".shstrtab"
};
static const char expecteddata[] =
{
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x7f,
0x81, 0x01, 0x81, 0x01, 0xff, 0xfe, 0xff, 0xff, 0x0f, 0xff, 0x7e, 0xfe,
0x03, 0xfe, 0x03, 0x82, 0xfc, 0xff, 0xff, 0x0f, 0x82, 0x7c, 0xd0, 0x0f,
0xd0, 0x0f, 0xb0, 0xf0, 0xff, 0xff, 0x0f, 0xb0, 0x70, 0xe8, 0x81, 0x02,
0xe8, 0x81, 0x02, 0x98, 0xfe, 0xfd, 0xff, 0x0f, 0x98, 0xfe, 0x7d, 0xff,
0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0x3f, 0x81, 0x80, 0x80, 0xc0, 0x0f,
0x81, 0x80, 0x80, 0x40, 0xff, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
0xff, 0x07, 0x81, 0x80, 0x80, 0x80, 0x08, 0x81, 0x80, 0x80, 0x80, 0x78
};
int
main (void)
{
AsmCtx_t *ctx;
AsmScn_t *scn;
int result = 0;
int fd;
Elf *elf;
GElf_Ehdr ehdr_mem;
GElf_Ehdr *ehdr;
size_t cnt;
elf_version (EV_CURRENT);
ctx = asm_begin (fname, false, EM_386, ELFCLASS32, ELFDATA2LSB);
if (ctx == NULL)
{
printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
return 1;
}
/* Create two sections. */
scn = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
if (scn == NULL)
{
printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
asm_abort (ctx);
return 1;
}
/* Special alignment for the .text section. */
if (asm_align (scn, 16) != 0)
{
printf ("cannot align .text section: %s\n", asm_errmsg (-1));
result = 1;
}
/* Add a few ULEB128 and SLEB128 numbers. */
for (cnt = 0; cnt < ninput; ++cnt)
{
if (asm_adduleb128 (scn, input[cnt]) != 0)
{
printf ("cannot insert uleb %" PRIu32 ": %s\n",
(uint32_t) input[cnt], asm_errmsg (-1));
result = 1;
}
if (asm_addsleb128 (scn, input[cnt]) != 0)
{
printf ("cannot insert sleb %" PRId32 ": %s\n",
input[cnt], asm_errmsg (-1));
result = 1;
}
if (asm_adduleb128 (scn, -input[cnt]) != 0)
{
printf ("cannot insert uleb %" PRIu32 ": %s\n",
(uint32_t) -input[cnt], asm_errmsg (-1));
result = 1;
}
if (asm_addsleb128 (scn, -input[cnt]) != 0)
{
printf ("cannot insert sleb %" PRId32 ": %s\n",
-input[cnt], asm_errmsg (-1));
result = 1;
}
}
/* Create the output file. */
if (asm_end (ctx) != 0)
{
printf ("cannot create output file: %s\n", asm_errmsg (-1));
asm_abort (ctx);
return 1;
}
/* Check the file. */
fd = open (fname, O_RDONLY);
if (fd == -1)
{
printf ("cannot open generated file: %m\n");
result = 1;
goto out;
}
elf = elf_begin (fd, ELF_C_READ, NULL);
if (elf == NULL)
{
printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
result = 1;
goto out_close;
}
if (elf_kind (elf) != ELF_K_ELF)
{
puts ("not a valid ELF file");
result = 1;
goto out_close2;
}
ehdr = gelf_getehdr (elf, &ehdr_mem);
if (ehdr == NULL)
{
printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
result = 1;
goto out_close2;
}
if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
{
puts ("ELF header does not match");
result = 1;
goto out_close2;
}
for (cnt = 1; cnt < 3; ++cnt)
{
Elf_Scn *scn;
GElf_Shdr shdr_mem;
GElf_Shdr *shdr;
scn = elf_getscn (elf, cnt);
if (scn == NULL)
{
printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
result = 1;
continue;
}
shdr = gelf_getshdr (scn, &shdr_mem);
if (shdr == NULL)
{
printf ("cannot get section header for section %Zd: %s\n",
cnt, elf_errmsg (-1));
result = 1;
continue;
}
if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
scnnames[cnt]) != 0)
{
printf ("section %Zd's name differs: %s vs %s\n", cnt,
elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
scnnames[cnt]);
result = 1;
}
if (shdr->sh_type != (cnt == 2 ? SHT_STRTAB : SHT_PROGBITS))
{
printf ("section %Zd's type differs\n", cnt);
result = 1;
}
if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
|| (cnt == 2 && shdr->sh_flags != 0))
{
printf ("section %Zd's flags differs\n", cnt);
result = 1;
}
if (shdr->sh_addr != 0)
{
printf ("section %Zd's address differs\n", cnt);
result = 1;
}
if ((cnt == 1 && shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 15) & ~15))
|| (cnt == 2
&& shdr->sh_offset != (((sizeof (Elf32_Ehdr) + 15) & ~15)
+ sizeof (expecteddata))))
{
printf ("section %Zd's offset differs\n", cnt);
result = 1;
}
if ((cnt == 1 && shdr->sh_size != sizeof (expecteddata))
|| (cnt == 2 && shdr->sh_size != 17))
{
printf ("section %Zd's size differs\n", cnt);
result = 1;
}
if (shdr->sh_link != 0)
{
printf ("section %Zd's link differs\n", cnt);
result = 1;
}
if (shdr->sh_info != 0)
{
printf ("section %Zd's info differs\n", cnt);
result = 1;
}
if ((cnt == 1 && shdr->sh_addralign != 16)
|| (cnt != 1 && shdr->sh_addralign != 1))
{
printf ("section %Zd's addralign differs\n", cnt);
result = 1;
}
if (shdr->sh_entsize != 0)
{
printf ("section %Zd's entsize differs\n", cnt);
result = 1;
}
if (cnt == 1)
{
Elf_Data *data = elf_getdata (scn, NULL);
if (data == NULL)
{
printf ("cannot get data of section %Zd\n", cnt);
result = 1;
}
else
{
if (data->d_size != sizeof (expecteddata))
{
printf ("data block size of section %Zd wrong: got %Zd, "
"expected 96\n", cnt, data->d_size);
result = 1;
}
if (memcmp (data->d_buf, expecteddata, sizeof (expecteddata))
!= 0)
{
printf ("data block content of section %Zd wrong\n", cnt);
result = 1;
}
}
}
}
out_close2:
elf_end (elf);
out_close:
close (fd);
out:
/* We don't need the file anymore. */
unlink (fname);
return result;
}