How to write an array var into .s file without null-terminator

Ian Lance Taylor iant@google.com
Wed Dec 12 05:25:00 GMT 2012


On Tue, Dec 11, 2012 at 5:27 PM, Dmitry Mikushin <dmitry@kernelgen.org> wrote:
>
> We are trying to embed a raw vector of chars into .s file using the
> following code:
>
>         tree index_type = build_index_type(size_int(moduleBitcode.size()));
>         tree const_char_type = build_qualified_type(
>             unsigned_char_type_node, TYPE_QUAL_CONST);
>         tree string_type = build_array_type(const_char_type, index_type);
>         string_type = build_variant_type_copy(string_type);
>         tree string_val = build_string(moduleBitcode.size(),
> moduleBitcode.data());
>         TREE_TYPE(string_val) = string_type;
>
>         tree var = create_tmp_var_raw(string_type, NULL);
>         char* tmpname = tmpnam(NULL) + strlen(P_tmpdir);
>         if (*tmpname == '/') tmpname++;
>         DECL_NAME (var) = get_identifier (tmpname);
>         DECL_SECTION_NAME (var) = build_string (11, ".kernelgen");
>         TREE_PUBLIC (var) = 1;
>         TREE_STATIC (var) = 1;
>         TREE_READONLY (var) = 1;
>         DECL_INITIAL (var) = string_val;
>         varpool_finalize_decl (var);
>
> The problem is that build_string always null-terminates our data array,
> which is unexpected. I tried to clone build_string and modify it in such way
> that it does not add '\0' in the end. But even in this case the output
> object size is moduleBitcode.size() + 1. Why? How to change this code to
> write the data exactly as it is - without null-terminator?

Well, don't use build_string.  Use build_constructor, or one of its
variants, and fill in the elements byte by byte.

Ian



More information about the Gcc mailing list