Armando Montanez | 6954a8d | 2021-07-19 21:11:28 -0700 | [diff] [blame] | 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 | #include <span> |
| 20 | |
| 21 | #include "pw_preprocessor/compiler.h" |
| 22 | |
| 23 | extern "C" const uint8_t gnu_build_id_begin; |
| 24 | |
| 25 | namespace pw::build_info { |
| 26 | namespace { |
| 27 | |
| 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 | std::span<const std::byte> BuildId() { |
| 37 | // Read the sizes at the beginning of the note section. |
| 38 | ElfNoteInfo build_id_note_sizes; |
| 39 | memcpy( |
| 40 | &build_id_note_sizes, &gnu_build_id_begin, sizeof(build_id_note_sizes)); |
| 41 | // Skip the "name" entry of the note section, and return a span to the |
| 42 | // descriptor. |
| 43 | return std::as_bytes(std::span(&gnu_build_id_begin + |
| 44 | sizeof(build_id_note_sizes) + |
| 45 | build_id_note_sizes.name_size, |
| 46 | build_id_note_sizes.descriptor_size)); |
| 47 | } |
| 48 | |
| 49 | } // namespace pw::build_info |