r"""read(address, count) -> bytes Read data from the specified virtual address Arguments: address(int): Virtual address to read count(int): Number of bytes to read Returns: A :class:`bytes` object, or :const:`None`. Examples:
(self, address, count)
| 1414 | return segment.header.p_offset + offset |
| 1415 | |
| 1416 | def read(self, address, count): |
| 1417 | r"""read(address, count) -> bytes |
| 1418 | |
| 1419 | Read data from the specified virtual address |
| 1420 | |
| 1421 | Arguments: |
| 1422 | address(int): Virtual address to read |
| 1423 | count(int): Number of bytes to read |
| 1424 | |
| 1425 | Returns: |
| 1426 | A :class:`bytes` object, or :const:`None`. |
| 1427 | |
| 1428 | Examples: |
| 1429 | The simplest example is just to read the ELF header. |
| 1430 | |
| 1431 | >>> bash = ELF(which('bash')) |
| 1432 | >>> bash.read(bash.address, 4) |
| 1433 | b'\x7fELF' |
| 1434 | |
| 1435 | ELF segments do not have to contain all of the data on-disk |
| 1436 | that gets loaded into memory. |
| 1437 | |
| 1438 | First, let's create an ELF file has some code in two sections. |
| 1439 | |
| 1440 | >>> assembly = ''' |
| 1441 | ... .section .A,"awx" |
| 1442 | ... .global A |
| 1443 | ... A: nop |
| 1444 | ... .section .B,"awx" |
| 1445 | ... .global B |
| 1446 | ... B: int3 |
| 1447 | ... ''' |
| 1448 | >>> e = ELF.from_assembly(assembly, vma=False) |
| 1449 | |
| 1450 | By default, these come right after eachother in memory. |
| 1451 | |
| 1452 | >>> e.read(e.symbols.A, 2) |
| 1453 | b'\x90\xcc' |
| 1454 | >>> e.symbols.B - e.symbols.A |
| 1455 | 1 |
| 1456 | |
| 1457 | Let's move the sections so that B is a little bit further away. |
| 1458 | |
| 1459 | >>> objcopy = pwnlib.asm._objcopy() |
| 1460 | >>> objcopy += [ |
| 1461 | ... '--change-section-vma', '.B+5', |
| 1462 | ... '--change-section-lma', '.B+5', |
| 1463 | ... e.path |
| 1464 | ... ] |
| 1465 | >>> subprocess.check_call(objcopy) |
| 1466 | 0 |
| 1467 | |
| 1468 | Now let's re-load the ELF, and check again |
| 1469 | |
| 1470 | >>> e = ELF(e.path) |
| 1471 | >>> e.symbols.B - e.symbols.A |
| 1472 | 6 |
| 1473 | >>> e.read(e.symbols.A, 2) |
no test coverage detected