• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
asm_addstrz(AsmScn_t * asmscn,const char * str,size_t len)43 asm_addstrz (AsmScn_t *asmscn, const char *str, size_t len)
44 {
45   if (asmscn == NULL)
46     return -1;
47 
48   if (unlikely (asmscn->type == SHT_NOBITS))
49     {
50       if (len == 0)
51 	{
52 	  if (str[0] != '\0')
53 	    {
54 	      __libasm_seterrno (ASM_E_TYPE);
55 	      return -1;
56 	    }
57 	}
58       else
59 	{
60 	  size_t cnt;
61 
62 	  for (cnt = 0; cnt < len; ++cnt)
63 	    if (str[cnt] != '\0')
64 	      {
65 		__libasm_seterrno (ASM_E_TYPE);
66 		return -1;
67 	      }
68 	}
69     }
70 
71   if (len == 0)
72     len = strlen (str) + 1;
73 
74   if (unlikely (asmscn->ctx->textp))
75     {
76       bool nextline = true;
77 
78       do
79 	{
80 	  if (nextline)
81 	    {
82 	      fputs ("\t.string\t\"", asmscn->ctx->out.file);
83 	      nextline = false;
84 	    }
85 
86 	  if (*str == '\0')
87 	    fputs ("\\000", asmscn->ctx->out.file);
88 	  else if (! isascii (*str))
89 	    fprintf (asmscn->ctx->out.file, "\\%03o",
90 		     (unsigned int) *((unsigned char *)str));
91 	  else if (*str == '\\')
92 	    fputs ("\\\\", asmscn->ctx->out.file);
93 	  else if (*str == '\n')
94 	    {
95 	      fputs ("\\n\"", asmscn->ctx->out.file);
96 	      nextline = true;
97 	    }
98 	  else
99 	    fputc (*str, asmscn->ctx->out.file);
100 
101 	  ++str;
102 	}
103       while (--len > 0 && (len > 1 || *str != '\0'));
104 
105       if (! nextline)
106 	fputs ("\"\n", asmscn->ctx->out.file);
107     }
108   else
109     {
110       /* Make sure there is enough room.  */
111       if (__libasm_ensure_section_space (asmscn, len) != 0)
112 	return -1;
113 
114       /* Copy the string.  */
115       memcpy (&asmscn->content->data[asmscn->content->len], str, len);
116 
117       /* Adjust the pointer in the data buffer.  */
118       asmscn->content->len += len;
119 
120       /* Increment the offset in the (sub)section.  */
121       asmscn->offset += len;
122     }
123 
124   return 0;
125 }
126