Skip to content

Commit

Permalink
Mem write odd (#730)
Browse files Browse the repository at this point in the history
* Use local variable for read_result instead of *ret, and fix
calculation of *ret for EOF case.

* Found a problem when reading an odd (%4) number of bytes at the end
of a file.  fread (on stm32) get them (say 3 bytes), then askes for
more.  do_semihosting gets a read return of 0 and tries to write that.
mem_write alters the address to be aligned and overwrites then 3 bytes
from the last read.

This change simply tells mem_write to do nothing if len is 0.

* Fix Issues from Fabien-Chouteau's review of my previous patch in isue #727.

* Revert change to mem_write() so it does not confuse fixes to do_semihosting().

* Add cast to avoid warning.

* Restore change to mem_write to return immeadiately if len == 0.
Add more comments on further possible issues and ways to handle them.
Using a branch to separate this change from others as it may need
more discussion and go on for a while...

* Remove cast of "-1" to uint32_t.  It's now compared to a ssize_t
and the compiler should be fine with that without any cast.
  • Loading branch information
donmr authored and xor-gate committed Aug 1, 2018
1 parent ea98ab7 commit 27ce268
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/gdbserver/semihosting.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ static int mem_read(stlink_t *sl, uint32_t addr, void *data, uint16_t len)

static int mem_write(stlink_t *sl, uint32_t addr, void *data, uint16_t len)
{
// Note: this function can write more than it is asked to!
// If addr is not an even 32 bit boundary, or len is not a multiple of 4.
//
// If only 32 bit values can be written to the target,
// then this function should read the target memory at the
// start and end of the buffer where it will write more that
// the requested bytes. (perhaps reading the whole area is faster??).
//
// If 16 and 8 bit writes are available, then they could be used instead.

// Just return when the length is zero avoiding unneeded work.
if (len == 0) return 0;

int offset = addr % 4;
int write_len = len + offset;

Expand Down

0 comments on commit 27ce268

Please sign in to comment.