Line data Source code
1 : /* Add string to a section.
2 : Copyright (C) 2002 Red Hat, Inc.
3 : This file is part of elfutils.
4 : Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5 :
6 : This file is free software; you can redistribute it and/or modify
7 : it under the terms of either
8 :
9 : * 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
22 : 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 :
26 : 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/>. */
29 :
30 : #ifdef HAVE_CONFIG_H
31 : # include <config.h>
32 : #endif
33 :
34 : #include <ctype.h>
35 : #include <stdio.h>
36 : #include <string.h>
37 :
38 : #include <libasmP.h>
39 :
40 :
41 : /* Add zero terminated string STR of size LEN to (sub)section ASMSCN. */
42 : int
43 6 : asm_addstrz (AsmScn_t *asmscn, const char *str, size_t len)
44 : {
45 6 : if (asmscn == NULL)
46 : return -1;
47 :
48 6 : if (unlikely (asmscn->type == SHT_NOBITS))
49 : {
50 0 : if (len == 0)
51 : {
52 0 : if (str[0] != '\0')
53 : {
54 0 : __libasm_seterrno (ASM_E_TYPE);
55 0 : return -1;
56 : }
57 : }
58 : else
59 : {
60 : size_t cnt;
61 :
62 0 : for (cnt = 0; cnt < len; ++cnt)
63 0 : if (str[cnt] != '\0')
64 : {
65 0 : __libasm_seterrno (ASM_E_TYPE);
66 0 : return -1;
67 : }
68 : }
69 : }
70 :
71 6 : if (len == 0)
72 2 : len = strlen (str) + 1;
73 :
74 6 : if (unlikely (asmscn->ctx->textp))
75 : {
76 : bool nextline = true;
77 :
78 : do
79 : {
80 0 : if (nextline)
81 : {
82 0 : fputs ("\t.string\t\"", asmscn->ctx->out.file);
83 0 : nextline = false;
84 : }
85 :
86 0 : if (*str == '\0')
87 0 : fputs ("\\000", asmscn->ctx->out.file);
88 0 : else if (! isascii (*str))
89 0 : fprintf (asmscn->ctx->out.file, "\\%03o",
90 : (unsigned int) *((unsigned char *)str));
91 0 : else if (*str == '\\')
92 0 : fputs ("\\\\", asmscn->ctx->out.file);
93 0 : else if (*str == '\n')
94 : {
95 0 : fputs ("\\n\"", asmscn->ctx->out.file);
96 0 : nextline = true;
97 : }
98 : else
99 0 : fputc (*str, asmscn->ctx->out.file);
100 :
101 0 : ++str;
102 : }
103 0 : while (--len > 0 && (len > 1 || *str != '\0'));
104 :
105 0 : if (! nextline)
106 0 : fputs ("\"\n", asmscn->ctx->out.file);
107 : }
108 : else
109 : {
110 : /* Make sure there is enough room. */
111 6 : if (__libasm_ensure_section_space (asmscn, len) != 0)
112 : return -1;
113 :
114 : /* Copy the string. */
115 12 : memcpy (&asmscn->content->data[asmscn->content->len], str, len);
116 :
117 : /* Adjust the pointer in the data buffer. */
118 6 : asmscn->content->len += len;
119 :
120 : /* Increment the offset in the (sub)section. */
121 6 : asmscn->offset += len;
122 : }
123 :
124 : return 0;
125 : }
|