Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Add string to a section. |
| 2 | Copyright (C) 2002 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 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 | asm_addstrz (asmscn, str, len) |
| 44 | AsmScn_t *asmscn; |
| 45 | const char *str; |
| 46 | size_t len; |
| 47 | { |
| 48 | if (asmscn == NULL) |
| 49 | return -1; |
| 50 | |
| 51 | if (unlikely (asmscn->type == SHT_NOBITS)) |
| 52 | { |
| 53 | if (len == 0) |
| 54 | { |
| 55 | if (str[0] != '\0') |
| 56 | { |
| 57 | __libasm_seterrno (ASM_E_TYPE); |
| 58 | return -1; |
| 59 | } |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | size_t cnt; |
| 64 | |
| 65 | for (cnt = 0; cnt < len; ++cnt) |
| 66 | if (str[cnt] != '\0') |
| 67 | { |
| 68 | __libasm_seterrno (ASM_E_TYPE); |
| 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (len == 0) |
| 75 | len = strlen (str) + 1; |
| 76 | |
| 77 | if (unlikely (asmscn->ctx->textp)) |
| 78 | { |
| 79 | bool nextline = true; |
| 80 | |
| 81 | do |
| 82 | { |
| 83 | if (nextline) |
| 84 | { |
| 85 | fputs ("\t.string\t\"", asmscn->ctx->out.file); |
| 86 | nextline = false; |
| 87 | } |
| 88 | |
| 89 | if (*str == '\0') |
| 90 | fputs ("\\000", asmscn->ctx->out.file); |
| 91 | else if (! isascii (*str)) |
| 92 | fprintf (asmscn->ctx->out.file, "\\%03o", |
| 93 | (unsigned int) *((unsigned char *)str)); |
| 94 | else if (*str == '\\') |
| 95 | fputs ("\\\\", asmscn->ctx->out.file); |
| 96 | else if (*str == '\n') |
| 97 | { |
| 98 | fputs ("\\n\"", asmscn->ctx->out.file); |
| 99 | nextline = true; |
| 100 | } |
| 101 | else |
| 102 | fputc (*str, asmscn->ctx->out.file); |
| 103 | |
| 104 | ++str; |
| 105 | } |
| 106 | while (--len > 0 && (len > 1 || *str != '\0')); |
| 107 | |
| 108 | if (! nextline) |
| 109 | fputs ("\"\n", asmscn->ctx->out.file); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | /* Make sure there is enough room. */ |
| 114 | if (__libasm_ensure_section_space (asmscn, len) != 0) |
| 115 | return -1; |
| 116 | |
| 117 | /* Copy the string. */ |
| 118 | memcpy (&asmscn->content->data[asmscn->content->len], str, len); |
| 119 | |
| 120 | /* Adjust the pointer in the data buffer. */ |
| 121 | asmscn->content->len += len; |
| 122 | |
| 123 | /* Increment the offset in the (sub)section. */ |
| 124 | asmscn->offset += len; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |