1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_build_info/build_id.h"
16
17 #include <cstdint>
18 #include <cstring>
19
20 #include "pw_preprocessor/compiler.h"
21 #include "pw_span/span.h"
22
23 extern "C" const uint8_t gnu_build_id_begin;
24
25 namespace pw::build_info {
26 namespace {
27
PW_PACKED(struct)28 PW_PACKED(struct) ElfNoteInfo {
29 uint32_t name_size;
30 uint32_t descriptor_size;
31 uint32_t type;
32 };
33
34 } // namespace
35
36 // Reading more than a uint8_t from gnu_build_id_begin triggers compiler
37 // warnings that must be silenced.
38 PW_MODIFY_DIAGNOSTICS_PUSH();
39 PW_MODIFY_DIAGNOSTIC(ignored, "-Warray-bounds");
40 PW_MODIFY_DIAGNOSTIC_GCC(ignored, "-Wstringop-overflow");
41 #if __GNUC__ >= 11
42 PW_MODIFY_DIAGNOSTIC_GCC(ignored, "-Wstringop-overread");
43 #endif
44
BuildId()45 span<const std::byte> BuildId() {
46 // Read the sizes at the beginning of the note section.
47 ElfNoteInfo build_id_note_sizes;
48 memcpy(
49 &build_id_note_sizes, &gnu_build_id_begin, sizeof(build_id_note_sizes));
50 // Skip the "name" entry of the note section, and return a span to the
51 // descriptor.
52 return as_bytes(span(&gnu_build_id_begin + sizeof(build_id_note_sizes) +
53 build_id_note_sizes.name_size,
54 build_id_note_sizes.descriptor_size));
55 }
56
57 PW_MODIFY_DIAGNOSTICS_POP();
58
59 } // namespace pw::build_info
60