blob: ec05b8da9216c5b7c3371180ff26de94489bcc6f [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Add integer to a section.
Ulrich Dreppera38998e2005-08-03 02:05:39 +00002 Copyright (C) 2002, 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
25
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <byteswap.h>
35#include <endian.h>
36#include <inttypes.h>
37#include <string.h>
38
39#include <libasmP.h>
40
41#ifndef SIZE
42# define SIZE 8
43#endif
44
45#define FCT(size) _FCT(size)
46#define _FCT(size) asm_addint##size
47#define TYPE(size) _TYPE(size)
48#define _TYPE(size) int##size##_t
49#define BSWAP(size) _BSWAP(size)
50#define _BSWAP(size) bswap_##size
51
52
53int
54FCT(SIZE) (asmscn, num)
55 AsmScn_t *asmscn;
56 TYPE(SIZE) num;
57{
58 if (asmscn == NULL)
59 return -1;
60
61 if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
62 {
63 __libasm_seterrno (ASM_E_TYPE);
64 return -1;
65 }
66
67 if (unlikely (asmscn->ctx->textp))
68 {
69 // XXX Needs to use backend specified pseudo-ops
70 if (SIZE == 8)
Ulrich Dreppera38998e2005-08-03 02:05:39 +000071 fprintf (asmscn->ctx->out.file, "\t.byte\t%" PRId8 "\n", (int8_t) num);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000072 else if (SIZE == 16)
Ulrich Dreppera38998e2005-08-03 02:05:39 +000073 fprintf (asmscn->ctx->out.file, "\t.value\t%" PRId16 "\n",
74 (int16_t) num);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000075 else if (SIZE == 32)
Ulrich Dreppera38998e2005-08-03 02:05:39 +000076 fprintf (asmscn->ctx->out.file, "\t.long\t%" PRId32 "\n",
77 (int32_t) num);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000078 else
79 {
80 // XXX This is not necessary for 64-bit machines
81 bool is_leb = (elf_getident (asmscn->ctx->out.elf, NULL)[EI_DATA]
82 == ELFDATA2LSB);
83
Ulrich Dreppera38998e2005-08-03 02:05:39 +000084 fprintf (asmscn->ctx->out.file,
85 "\t.long\t%" PRId32 "\n\t.long\t%" PRId32 "\n",
86 (int32_t) (is_leb
87 ? num % 0x100000000ll : num / 0x100000000ll),
88 (int32_t) (is_leb
89 ? num / 0x100000000ll : num % 0x100000000ll));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000090 }
91 }
92 else
93 {
94#if SIZE > 8
95 bool is_leb = (elf_getident (asmscn->ctx->out.elf, NULL)[EI_DATA]
96 == ELFDATA2LSB);
97#endif
98 TYPE(SIZE) var = num;
99
100 /* Make sure we have enough room. */
101 if (__libasm_ensure_section_space (asmscn, SIZE / 8) != 0)
102 return -1;
103
104#if SIZE > 8
105 if ((BYTE_ORDER == LITTLE_ENDIAN && !is_leb)
106 || (BYTE_ORDER == BIG_ENDIAN && is_leb))
107 var = BSWAP(SIZE) (var);
108#endif
109
110 /* Copy the variable value. */
111 if (likely (asmscn->type == SHT_NOBITS))
112 memcpy (&asmscn->content->data[asmscn->content->len], &var, SIZE / 8);
113
114 /* Adjust the pointer in the data buffer. */
115 asmscn->content->len += SIZE / 8;
116
117 /* Increment the offset in the (sub)section. */
118 asmscn->offset += SIZE / 8;
119 }
120
121 return 0;
122}
123INTDEF(FCT(SIZE))