blob: ff4376b869c018597a21ce08dd77fe56353608ff [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.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Ulrich Drepper361df7d2006-04-04 21:38:57 +00006 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 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,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000017 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000018
19 Red Hat elfutils is an included package of the Open Invention Network.
20 An included package of the Open Invention Network is a package for which
21 Open Invention Network licensees cross-license their patents. No patent
22 license is granted, either expressly or impliedly, by designation as an
23 included package. Should you wish to participate in the Open Invention
24 Network licensing program, please visit www.openinventionnetwork.com
25 <http://www.openinventionnetwork.com>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000026
27#ifdef HAVE_CONFIG_H
28# include <config.h>
29#endif
30
31#include <byteswap.h>
32#include <endian.h>
33#include <inttypes.h>
34#include <string.h>
35
36#include <libasmP.h>
37
38#ifndef SIZE
39# define SIZE 8
40#endif
41
42#define FCT(size) _FCT(size)
43#define _FCT(size) asm_addint##size
44#define TYPE(size) _TYPE(size)
45#define _TYPE(size) int##size##_t
46#define BSWAP(size) _BSWAP(size)
47#define _BSWAP(size) bswap_##size
48
49
50int
51FCT(SIZE) (asmscn, num)
52 AsmScn_t *asmscn;
53 TYPE(SIZE) num;
54{
55 if (asmscn == NULL)
56 return -1;
57
58 if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
59 {
60 __libasm_seterrno (ASM_E_TYPE);
61 return -1;
62 }
63
64 if (unlikely (asmscn->ctx->textp))
65 {
66 // XXX Needs to use backend specified pseudo-ops
67 if (SIZE == 8)
Ulrich Dreppera38998e2005-08-03 02:05:39 +000068 fprintf (asmscn->ctx->out.file, "\t.byte\t%" PRId8 "\n", (int8_t) num);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000069 else if (SIZE == 16)
Ulrich Dreppera38998e2005-08-03 02:05:39 +000070 fprintf (asmscn->ctx->out.file, "\t.value\t%" PRId16 "\n",
71 (int16_t) num);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000072 else if (SIZE == 32)
Ulrich Dreppera38998e2005-08-03 02:05:39 +000073 fprintf (asmscn->ctx->out.file, "\t.long\t%" PRId32 "\n",
74 (int32_t) num);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000075 else
76 {
77 // XXX This is not necessary for 64-bit machines
78 bool is_leb = (elf_getident (asmscn->ctx->out.elf, NULL)[EI_DATA]
79 == ELFDATA2LSB);
80
Ulrich Dreppera38998e2005-08-03 02:05:39 +000081 fprintf (asmscn->ctx->out.file,
82 "\t.long\t%" PRId32 "\n\t.long\t%" PRId32 "\n",
83 (int32_t) (is_leb
84 ? num % 0x100000000ll : num / 0x100000000ll),
85 (int32_t) (is_leb
86 ? num / 0x100000000ll : num % 0x100000000ll));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000087 }
88 }
89 else
90 {
91#if SIZE > 8
92 bool is_leb = (elf_getident (asmscn->ctx->out.elf, NULL)[EI_DATA]
93 == ELFDATA2LSB);
94#endif
95 TYPE(SIZE) var = num;
96
97 /* Make sure we have enough room. */
98 if (__libasm_ensure_section_space (asmscn, SIZE / 8) != 0)
99 return -1;
100
101#if SIZE > 8
102 if ((BYTE_ORDER == LITTLE_ENDIAN && !is_leb)
103 || (BYTE_ORDER == BIG_ENDIAN && is_leb))
104 var = BSWAP(SIZE) (var);
105#endif
106
107 /* Copy the variable value. */
108 if (likely (asmscn->type == SHT_NOBITS))
109 memcpy (&asmscn->content->data[asmscn->content->len], &var, SIZE / 8);
110
111 /* Adjust the pointer in the data buffer. */
112 asmscn->content->len += SIZE / 8;
113
114 /* Increment the offset in the (sub)section. */
115 asmscn->offset += SIZE / 8;
116 }
117
118 return 0;
119}
120INTDEF(FCT(SIZE))