1/* Add integer to a section.
2   Copyright (C) 2002, 2005 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6   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.
9
10   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,
17   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
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>.  */
26
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)
68	fprintf (asmscn->ctx->out.file, "\t.byte\t%" PRId8 "\n", (int8_t) num);
69      else if (SIZE == 16)
70	fprintf (asmscn->ctx->out.file, "\t.value\t%" PRId16 "\n",
71		 (int16_t) num);
72      else if (SIZE == 32)
73	fprintf (asmscn->ctx->out.file, "\t.long\t%" PRId32 "\n",
74		 (int32_t) num);
75      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
81	  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));
87	}
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))
121