xref: /aosp_15_r20/external/pigweed/pw_tokenizer/pw_tokenizer_linker_sections.ld (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1/*
2 * Copyright 2020 The Pigweed Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 *     https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17/*
18 *
19 * This linker script snippet declares the sections needed for string
20 * tokenization. All sections have type INFO so they are excluded from the final
21 * binary.
22 *
23 * The contents of this script can be copied into an existing linker script.
24 * Alternately, this file can be directly included in a linker script with an
25 * include directive. For example,
26 *
27 *   INCLUDE path/to/modules/pw_tokenizer/pw_tokenizer_linker_sections.ld
28 *
29 *   SECTIONS
30 *   {
31 *     (your existing linker sections)
32 *   }
33 */
34
35SECTIONS
36{
37  /*
38   * This section stores metadata that may be used during tokenized string
39   * decoding. This metadata describes properties that may affect how the
40   * tokenized string is encoded or decoded -- the maximum length of the hash
41   * function and the sizes of certain integer types.
42   *
43   * Metadata is declared as key-value pairs. See the metadata variable in
44   * tokenize.cc for further details.
45   */
46  .pw_tokenizer.info 0x0 (INFO) :
47  {
48    KEEP(*(.pw_tokenizer.info))
49  }
50
51  /*
52   * Tokenized string entries are stored in this section. Each entry contains
53   * the original string literal and the calculated token that represents it. In
54   * the compiled code, the token and a compact argument list encoded in a
55   * uint32_t are used in place of the format string. The compiled code
56   * contains no references to the tokenized string entries in this section.
57   *
58   * The tokenized string entry format is specified by the
59   * pw::tokenizer::internal::Entry class in
60   * pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h.
61   *
62   * The section contents are declared with KEEP so that they are not removed
63   * from the ELF. These are never emitted in the final binary or loaded into
64   * memory.
65   */
66  .pw_tokenizer.entries 0x0 (INFO) :
67  {
68    KEEP(*(.pw_tokenizer.entries.*))
69    /* GCC has a known bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88061)
70     * that causes it to ignore any user-specified section placement for
71     * variables declared inside function templates. The symbols for these
72     * variables instead end up in a .rodata.* subsection. The subsection names
73     * for these symbols all contain the string "_pw_tokenizer_string_entry_"
74     * (as long as -fdata-sections was used when compiling). Thus we can pick
75     * the relevant sections by using an appropriate wildcard.
76     *
77     * Note that this technique only works because nothing in the source code
78     * references the *_pw_tokenizer_string_entry_* symbols. This ensures the
79     * linker will never place such symbols in the final .data (aka .rodata)
80     * output section, meaning such symbols remain available for us to place
81     * into the  .pw_tokenizer_entries section.
82     */
83    KEEP(*(.rodata.*_pw_tokenizer_string_entry_*))
84  }
85}
86