Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 1 | //===-- SBSection.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/API/SBSection.h" |
| 11 | #include "lldb/API/SBStream.h" |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBTarget.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Section.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/DataBuffer.h" |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame^] | 17 | #include "lldb/Utility/DataBufferLLVM.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/StreamString.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 21 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | SBSection::SBSection() : m_opaque_wp() {} |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {} |
| 28 | |
| 29 | SBSection::SBSection(const lldb::SectionSP §ion_sp) |
| 30 | : m_opaque_wp() // Don't init with section_sp otherwise this will throw if |
| 31 | // section_sp doesn't contain a valid Section * |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 32 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | if (section_sp) |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 34 | m_opaque_wp = section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | const SBSection &SBSection::operator=(const SBSection &rhs) { |
| 38 | m_opaque_wp = rhs.m_opaque_wp; |
| 39 | return *this; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | SBSection::~SBSection() {} |
| 43 | |
| 44 | bool SBSection::IsValid() const { |
| 45 | SectionSP section_sp(GetSP()); |
| 46 | return section_sp && section_sp->GetModule().get() != NULL; |
| 47 | } |
| 48 | |
| 49 | const char *SBSection::GetName() { |
| 50 | SectionSP section_sp(GetSP()); |
| 51 | if (section_sp) |
| 52 | return section_sp->GetName().GetCString(); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | lldb::SBSection SBSection::GetParent() { |
| 57 | lldb::SBSection sb_section; |
| 58 | SectionSP section_sp(GetSP()); |
| 59 | if (section_sp) { |
| 60 | SectionSP parent_section_sp(section_sp->GetParent()); |
| 61 | if (parent_section_sp) |
| 62 | sb_section.SetSP(parent_section_sp); |
| 63 | } |
| 64 | return sb_section; |
| 65 | } |
| 66 | |
| 67 | lldb::SBSection SBSection::FindSubSection(const char *sect_name) { |
| 68 | lldb::SBSection sb_section; |
| 69 | if (sect_name) { |
| 70 | SectionSP section_sp(GetSP()); |
| 71 | if (section_sp) { |
| 72 | ConstString const_sect_name(sect_name); |
| 73 | sb_section.SetSP( |
| 74 | section_sp->GetChildren().FindSectionByName(const_sect_name)); |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 75 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | } |
| 77 | return sb_section; |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | size_t SBSection::GetNumSubSections() { |
| 81 | SectionSP section_sp(GetSP()); |
| 82 | if (section_sp) |
| 83 | return section_sp->GetChildren().GetSize(); |
| 84 | return 0; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) { |
| 88 | lldb::SBSection sb_section; |
| 89 | SectionSP section_sp(GetSP()); |
| 90 | if (section_sp) |
| 91 | sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx)); |
| 92 | return sb_section; |
| 93 | } |
| 94 | |
| 95 | lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); } |
| 96 | |
| 97 | void SBSection::SetSP(const lldb::SectionSP §ion_sp) { |
| 98 | m_opaque_wp = section_sp; |
| 99 | } |
| 100 | |
| 101 | lldb::addr_t SBSection::GetFileAddress() { |
| 102 | lldb::addr_t file_addr = LLDB_INVALID_ADDRESS; |
| 103 | SectionSP section_sp(GetSP()); |
| 104 | if (section_sp) |
| 105 | return section_sp->GetFileAddress(); |
| 106 | return file_addr; |
| 107 | } |
| 108 | |
| 109 | lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) { |
| 110 | TargetSP target_sp(sb_target.GetSP()); |
| 111 | if (target_sp) { |
| 112 | SectionSP section_sp(GetSP()); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 113 | if (section_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | return section_sp->GetLoadBaseAddress(target_sp.get()); |
| 115 | } |
| 116 | return LLDB_INVALID_ADDRESS; |
| 117 | } |
| 118 | |
| 119 | lldb::addr_t SBSection::GetByteSize() { |
| 120 | SectionSP section_sp(GetSP()); |
| 121 | if (section_sp) |
| 122 | return section_sp->GetByteSize(); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | uint64_t SBSection::GetFileOffset() { |
| 127 | SectionSP section_sp(GetSP()); |
| 128 | if (section_sp) { |
| 129 | ModuleSP module_sp(section_sp->GetModule()); |
| 130 | if (module_sp) { |
| 131 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 132 | if (objfile) |
| 133 | return objfile->GetFileOffset() + section_sp->GetFileOffset(); |
| 134 | } |
| 135 | } |
| 136 | return UINT64_MAX; |
| 137 | } |
| 138 | |
| 139 | uint64_t SBSection::GetFileByteSize() { |
| 140 | SectionSP section_sp(GetSP()); |
| 141 | if (section_sp) |
| 142 | return section_sp->GetFileSize(); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); } |
| 147 | |
| 148 | SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) { |
| 149 | SBData sb_data; |
| 150 | SectionSP section_sp(GetSP()); |
| 151 | if (section_sp) { |
| 152 | const uint64_t sect_file_size = section_sp->GetFileSize(); |
| 153 | if (sect_file_size > 0) { |
| 154 | ModuleSP module_sp(section_sp->GetModule()); |
| 155 | if (module_sp) { |
| 156 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 157 | if (objfile) { |
| 158 | const uint64_t sect_file_offset = |
| 159 | objfile->GetFileOffset() + section_sp->GetFileOffset(); |
| 160 | const uint64_t file_offset = sect_file_offset + offset; |
| 161 | uint64_t file_size = size; |
| 162 | if (file_size == UINT64_MAX) { |
| 163 | file_size = section_sp->GetByteSize(); |
| 164 | if (file_size > offset) |
| 165 | file_size -= offset; |
| 166 | else |
| 167 | file_size = 0; |
| 168 | } |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame^] | 169 | auto data_buffer_sp = DataBufferLLVM::CreateSliceFromPath( |
| 170 | objfile->GetFileSpec().GetPath(), file_size, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) { |
| 172 | DataExtractorSP data_extractor_sp( |
| 173 | new DataExtractor(data_buffer_sp, objfile->GetByteOrder(), |
| 174 | objfile->GetAddressByteSize())); |
| 175 | |
| 176 | sb_data.SetOpaque(data_extractor_sp); |
| 177 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 178 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 180 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | } |
| 182 | return sb_data; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | SectionType SBSection::GetSectionType() { |
| 186 | SectionSP section_sp(GetSP()); |
| 187 | if (section_sp.get()) |
| 188 | return section_sp->GetType(); |
| 189 | return eSectionTypeInvalid; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Abhishek Aggarwal | 3c7f070 | 2016-09-08 12:22:56 +0000 | [diff] [blame] | 192 | uint32_t |
| 193 | SBSection::GetPermissions() const |
| 194 | { |
| 195 | SectionSP section_sp(GetSP()); |
| 196 | if (section_sp) |
| 197 | return section_sp->GetPermissions(); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | uint32_t SBSection::GetTargetByteSize() { |
| 202 | SectionSP section_sp(GetSP()); |
| 203 | if (section_sp.get()) |
| 204 | return section_sp->GetTargetByteSize(); |
| 205 | return 0; |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | bool SBSection::operator==(const SBSection &rhs) { |
| 209 | SectionSP lhs_section_sp(GetSP()); |
| 210 | SectionSP rhs_section_sp(rhs.GetSP()); |
| 211 | if (lhs_section_sp && rhs_section_sp) |
| 212 | return lhs_section_sp == rhs_section_sp; |
| 213 | return false; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | bool SBSection::operator!=(const SBSection &rhs) { |
| 217 | SectionSP lhs_section_sp(GetSP()); |
| 218 | SectionSP rhs_section_sp(rhs.GetSP()); |
| 219 | return lhs_section_sp != rhs_section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | bool SBSection::GetDescription(SBStream &description) { |
| 223 | Stream &strm = description.ref(); |
| 224 | |
| 225 | SectionSP section_sp(GetSP()); |
| 226 | if (section_sp) { |
| 227 | const addr_t file_addr = section_sp->GetFileAddress(); |
| 228 | strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, |
| 229 | file_addr + section_sp->GetByteSize()); |
| 230 | section_sp->DumpName(&strm); |
| 231 | } else { |
| 232 | strm.PutCString("No value"); |
| 233 | } |
| 234 | |
| 235 | return true; |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 236 | } |