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/DataBuffer.h" |
| 14 | #include "lldb/Core/DataExtractor.h" |
| 15 | #include "lldb/Core/Log.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Module.h" |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Section.h" |
| 18 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 20 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | |
| 26 | SBSection::SBSection () : |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 27 | m_opaque_wp () |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 28 | { |
| 29 | } |
| 30 | |
| 31 | SBSection::SBSection (const SBSection &rhs) : |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 32 | m_opaque_wp (rhs.m_opaque_wp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 33 | { |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
| 37 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 38 | SBSection::SBSection (const lldb::SectionSP §ion_sp) : |
| 39 | m_opaque_wp () // Don't init with section_sp otherwise this will throw if section_sp doesn't contain a valid Section * |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 40 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 41 | if (section_sp) |
| 42 | m_opaque_wp = section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | const SBSection & |
| 46 | SBSection::operator = (const SBSection &rhs) |
| 47 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 48 | m_opaque_wp = rhs.m_opaque_wp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | SBSection::~SBSection () |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | bool |
| 57 | SBSection::IsValid () const |
| 58 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 59 | SectionSP section_sp (GetSP()); |
| 60 | return section_sp && section_sp->GetModule().get() != NULL; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Greg Clayton | f644ddf | 2011-09-24 01:37:21 +0000 | [diff] [blame] | 63 | const char * |
| 64 | SBSection::GetName () |
| 65 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 66 | SectionSP section_sp (GetSP()); |
| 67 | if (section_sp) |
| 68 | return section_sp->GetName().GetCString(); |
Greg Clayton | f644ddf | 2011-09-24 01:37:21 +0000 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
| 71 | |
Greg Clayton | 1d4c540 | 2013-06-13 21:23:23 +0000 | [diff] [blame] | 72 | lldb::SBSection |
| 73 | SBSection::GetParent() |
| 74 | { |
| 75 | lldb::SBSection sb_section; |
| 76 | SectionSP section_sp (GetSP()); |
| 77 | if (section_sp) |
| 78 | { |
| 79 | SectionSP parent_section_sp (section_sp->GetParent()); |
| 80 | if (parent_section_sp) |
| 81 | sb_section.SetSP(parent_section_sp); |
| 82 | } |
| 83 | return sb_section; |
| 84 | } |
| 85 | |
Greg Clayton | f644ddf | 2011-09-24 01:37:21 +0000 | [diff] [blame] | 86 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 87 | lldb::SBSection |
| 88 | SBSection::FindSubSection (const char *sect_name) |
| 89 | { |
| 90 | lldb::SBSection sb_section; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 91 | if (sect_name) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 92 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 93 | SectionSP section_sp (GetSP()); |
| 94 | if (section_sp) |
| 95 | { |
| 96 | ConstString const_sect_name(sect_name); |
| 97 | sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name)); |
| 98 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 99 | } |
| 100 | return sb_section; |
| 101 | } |
| 102 | |
| 103 | size_t |
| 104 | SBSection::GetNumSubSections () |
| 105 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 106 | SectionSP section_sp (GetSP()); |
| 107 | if (section_sp) |
| 108 | return section_sp->GetChildren ().GetSize(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | lldb::SBSection |
| 113 | SBSection::GetSubSectionAtIndex (size_t idx) |
| 114 | { |
| 115 | lldb::SBSection sb_section; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 116 | SectionSP section_sp (GetSP()); |
| 117 | if (section_sp) |
| 118 | sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx)); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 119 | return sb_section; |
| 120 | } |
| 121 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 122 | lldb::SectionSP |
| 123 | SBSection::GetSP() const |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 124 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 125 | return m_opaque_wp.lock(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 129 | SBSection::SetSP(const lldb::SectionSP §ion_sp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 130 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 131 | m_opaque_wp = section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 134 | lldb::addr_t |
| 135 | SBSection::GetFileAddress () |
| 136 | { |
| 137 | lldb::addr_t file_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 138 | SectionSP section_sp (GetSP()); |
| 139 | if (section_sp) |
| 140 | return section_sp->GetFileAddress(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 141 | return file_addr; |
| 142 | } |
| 143 | |
| 144 | lldb::addr_t |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 145 | SBSection::GetLoadAddress (lldb::SBTarget &sb_target) |
| 146 | { |
| 147 | TargetSP target_sp(sb_target.GetSP()); |
| 148 | if (target_sp) |
| 149 | { |
| 150 | SectionSP section_sp (GetSP()); |
| 151 | if (section_sp) |
| 152 | return section_sp->GetLoadBaseAddress(target_sp.get()); |
| 153 | } |
| 154 | return LLDB_INVALID_ADDRESS; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | lldb::addr_t |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 161 | SBSection::GetByteSize () |
| 162 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 163 | SectionSP section_sp (GetSP()); |
| 164 | if (section_sp) |
| 165 | return section_sp->GetByteSize(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | uint64_t |
| 170 | SBSection::GetFileOffset () |
| 171 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 172 | SectionSP section_sp (GetSP()); |
| 173 | if (section_sp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 174 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 175 | ModuleSP module_sp (section_sp->GetModule()); |
| 176 | if (module_sp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 177 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 178 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 179 | if (objfile) |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 180 | return objfile->GetFileOffset() + section_sp->GetFileOffset(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 183 | return UINT64_MAX; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | uint64_t |
| 187 | SBSection::GetFileByteSize () |
| 188 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 189 | SectionSP section_sp (GetSP()); |
| 190 | if (section_sp) |
| 191 | return section_sp->GetFileSize(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | SBData |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 196 | SBSection::GetSectionData () |
| 197 | { |
| 198 | return GetSectionData (0, UINT64_MAX); |
| 199 | } |
| 200 | |
| 201 | SBData |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 202 | SBSection::GetSectionData (uint64_t offset, uint64_t size) |
| 203 | { |
| 204 | SBData sb_data; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 205 | SectionSP section_sp (GetSP()); |
| 206 | if (section_sp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 207 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 208 | const uint64_t sect_file_size = section_sp->GetFileSize(); |
| 209 | if (sect_file_size > 0) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 210 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 211 | ModuleSP module_sp (section_sp->GetModule()); |
| 212 | if (module_sp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 213 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 214 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 215 | if (objfile) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 216 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 217 | const uint64_t sect_file_offset = objfile->GetFileOffset() + section_sp->GetFileOffset(); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 218 | const uint64_t file_offset = sect_file_offset + offset; |
| 219 | uint64_t file_size = size; |
| 220 | if (file_size == UINT64_MAX) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 221 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 222 | file_size = section_sp->GetByteSize(); |
| 223 | if (file_size > offset) |
| 224 | file_size -= offset; |
| 225 | else |
| 226 | file_size = 0; |
| 227 | } |
| 228 | DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size)); |
| 229 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) |
| 230 | { |
| 231 | DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp, |
| 232 | objfile->GetByteOrder(), |
| 233 | objfile->GetAddressByteSize())); |
| 234 | |
| 235 | sb_data.SetOpaque (data_extractor_sp); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | return sb_data; |
| 242 | } |
| 243 | |
| 244 | SectionType |
| 245 | SBSection::GetSectionType () |
| 246 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 247 | SectionSP section_sp (GetSP()); |
| 248 | if (section_sp.get()) |
| 249 | return section_sp->GetType(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 250 | return eSectionTypeInvalid; |
| 251 | } |
| 252 | |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 253 | uint32_t |
| 254 | SBSection::GetTargetByteSize () |
| 255 | { |
| 256 | SectionSP section_sp (GetSP()); |
| 257 | if (section_sp.get()) |
| 258 | return section_sp->GetTargetByteSize(); |
| 259 | return 0; |
| 260 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 261 | |
| 262 | bool |
| 263 | SBSection::operator == (const SBSection &rhs) |
| 264 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 265 | SectionSP lhs_section_sp (GetSP()); |
| 266 | SectionSP rhs_section_sp (rhs.GetSP()); |
| 267 | if (lhs_section_sp && rhs_section_sp) |
| 268 | return lhs_section_sp == rhs_section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | bool |
| 273 | SBSection::operator != (const SBSection &rhs) |
| 274 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 275 | SectionSP lhs_section_sp (GetSP()); |
| 276 | SectionSP rhs_section_sp (rhs.GetSP()); |
| 277 | return lhs_section_sp != rhs_section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | bool |
| 281 | SBSection::GetDescription (SBStream &description) |
| 282 | { |
Greg Clayton | da7bc7d | 2011-11-13 06:57:31 +0000 | [diff] [blame] | 283 | Stream &strm = description.ref(); |
| 284 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 285 | SectionSP section_sp (GetSP()); |
| 286 | if (section_sp) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 287 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 288 | const addr_t file_addr = section_sp->GetFileAddress(); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 289 | strm.Printf ("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, file_addr + section_sp->GetByteSize()); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 290 | section_sp->DumpName(&strm); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 291 | } |
| 292 | else |
| 293 | { |
Greg Clayton | da7bc7d | 2011-11-13 06:57:31 +0000 | [diff] [blame] | 294 | strm.PutCString ("No value"); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |